Can this code get speeded up

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,240
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"
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,240
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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?
 

mahmed1

Well-known Member
Joined
Mar 28, 2009
Messages
2,240
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,126,901
Messages
5,621,564
Members
415,846
Latest member
Jrider1

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
Top