Hey all -
I have a number of queries that take a long time to execute (30-60 seconds). I'm hoping y'all can help me with one of them, and I can then apply that knowledge to the rest. I'm a native .NET programmer, so my SQL work generally is visual, not hard coded like this.
I know this is a pretty beefy query, but can anyone help me optimize this query for greater speed?
Thanks,
Scott
I have a number of queries that take a long time to execute (30-60 seconds). I'm hoping y'all can help me with one of them, and I can then apply that knowledge to the rest. I'm a native .NET programmer, so my SQL work generally is visual, not hard coded like this.
I know this is a pretty beefy query, but can anyone help me optimize this query for greater speed?
Code:
INSERT INTO lcltblPnL_DtlLabor ( Time_Main_Key, Time_Tech_ID, WF_Shop_ID, Time_Bonus, Time_Vacation, Time_Sick, Time_OtherAdd, Time_OtherDed, LABDolC, LARDolC, LAMDolC, LAFDolC, LASDolC, LADDolC, LAEDolC, LAGDolC, LABHrsPaid, LARHrsPaid, LAMHrsPaid, LAFHrsPaid, LASHrsPaid, LADHrsPaid, LAEHrsPaid, LAGHrsPaid )
SELECT lkpqryLaborCost.Time_Main_Key, lkpqryLaborCost.Time_Tech_ID, lkpqryLaborCost.WF_Shop_ID, lkpqryLaborCost.Time_Bonus, lkpqryLaborCost.Time_Vacation, lkpqryLaborCost.Time_Sick, lkpqryLaborCost.Time_OtherAdd, lkpqryLaborCost.Time_OtherDed, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LABRate])*[SumOfTime_LAB_P]) AS LABDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LARRate])*([SumOfTime_LAR_P])) AS LARDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LAMRate])*[SumOfTime_LAM_P]) AS LAMDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LAFRate])*[SumOfTime_LAF_P]) AS LAFDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LASRate])*[SumOfTime_LAS_P]) AS LASDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LADRate])*[SumOfTime_LAD_P]) AS LADDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LAERate])*[SumOfTime_LAE_P]) AS LAEDolC, Sum(IIf([TotalRate]<>0,[TotalRate],[Time_LAGRate])*[SumOfTime_LAG_P]) AS LAGDolC, Sum(lkpqryLaborCost.SumOfTime_LAB_P) AS LABHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAR_P) AS LARHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAM_P) AS LAMHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAF_P) AS LAFHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAS_P) AS LASHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAD_P) AS LADHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAE_P) AS LAEHrsPaid, Sum(lkpqryLaborCost.SumOfTime_LAG_P) AS LAGHrsPaid
FROM (lkpqryLaborCostRate INNER JOIN lkpqryLaborCost ON lkpqryLaborCostRate.Time_Main_Key = lkpqryLaborCost.Time_Main_Key) INNER JOIN lcltblPnL_List ON lkpqryLaborCost.WF_Shop_ID = lcltblPnL_List.WF_Shop_Key
GROUP BY lkpqryLaborCost.Time_Main_Key, lkpqryLaborCost.Time_Tech_ID, lkpqryLaborCost.WF_Shop_ID, lkpqryLaborCost.Time_Bonus, lkpqryLaborCost.Time_Vacation, lkpqryLaborCost.Time_Sick, lkpqryLaborCost.Time_OtherAdd, lkpqryLaborCost.Time_OtherDed;
Thanks,
Scott