Can this code get speeded up

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,302
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hiya

I've got this SQL statement in excel to pull back data from access into my excel sheet

This works fine but takes around 10-12 minutes to run each time and was hoping i can speed this up...I believe it may be the join or the In statement that makes it take long

Hopefully you can help me speed it up

VBA Code:
  SQLQuery = "SELECT t.Date AS [Date], t.Department AS Department, SUM(t.Requested) AS Requested, SUM(t.Connected) AS Connected, SUM(t.Requested) - SUM(t.Connected) AS Abandoned, MAX(ROUND(IIF(Requested > 0, Connected/Requested, 0), 2)) AS PWA, SUM(t.THT)/SUM(IIF(t.Connected>0,t.Connected,9.999999E+306)) AS AHT, SUM(t.TSA)/SUM(IIF(t.Connected>0,t.Connected,9.999999E+306)) AS ASA" & _

                        " FROM (SELECT roll.DATE AS [Date], lkup.Department As Department, IIF(SUM(roll.[REQUESTED CHATS])>0,SUM(roll.[REQUESTED CHATS]),0) AS Requested, IIF(SUM(roll.[CONNECTED CHATS])>0, SUM(roll.[CONNECTED CHATS]),0) AS Connected, SUM(roll.THT) AS THT, SUM(roll.TSA) AS TSA" & _

                        " FROM tbl_WebChatRolling AS roll INNER JOIN lookup_WebChat AS lkup ON lkup.Skill = roll.SKILL" & _
                        " WHERE roll.DATE Is Not Null And lkup.Department In " & Zx & " And ([Date] Between #" & fromDate & "# And #" & toDate & "#) And ((CStr(Format(roll.HOUR, 'hh:mm')) IN ('08:00',   '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00')) AND (CStr(Format(roll.Date, 'ddd')) IN ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')))" & _
                        " GROUP BY roll.DATE, lkup.Department" & _
                        " ORDER BY lkup.Department, roll.DATE" & _

                        " UNION ALL" & _

                        " SELECT roll.DATE AS [Date], lkup.Department AS Department, IIF(SUM(roll.[REQUESTED CHATS])>0,SUM(roll.[REQUESTED CHATS]),0) AS Requested, IIF(SUM(roll.[CONNECTED CHATS])>0, SUM(roll.[CONNECTED CHATS]),0) AS Connected, SUM(roll.THT) AS THT, SUM(roll.TSA) AS TSA" & _

                        " FROM tbl_WebChatRolling AS roll INNER JOIN lookup_WebChat_Night AS lkup ON lkup.Skill = roll.SKILL" & _
                        " WHERE roll.DATE Is Not Null And lkup.Department In " & Zx & " And ([Date] Between #" & fromDate & "# And #" & toDate & "#) And ((CStr(Format(roll.HOUR, 'hh:mm')) IN ('00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '21:00', '22:00', '23:00')) OR (CStr(Format(roll.Date, 'ddd')) IN ('Sun')))" & _
                        " GROUP BY roll.DATE, lkup.Department)  AS t" & _
                        " GROUP BY t.Date, t.Department" & _
                        " ORDER BY t.Date, t.Department"
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hiya - this wasnt set up by me but i can see no index - what fields would i need to add an index on to make it quicker and do i need to add index to query?
 
Upvote 0
I added indexes to Date and Skill with allowing dups and then also added index on the skill on lookup table with no dups and this has speeded up big time by under a couple of mins :) - Thank you
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top