I'm working on this stored proc and the thing is that with one of the options it take 5 minutes to run, but with all of the others it takes only 5 seconds.
DECLARE @rptType varchar(75)
SET @rptType = 'Audits'
SELECT CLT.[cltNum] AS [Client Number] ,CLT.[cltEng] AS [Engagement] ,CLT.[cltName] AS [Client Name] , CLT.[CSPLname] AS [Assurance Partner], CLT.[cmgLName], e.[DDEventDesc] AS [Project Type], Budget.[CBudProjectDesc] AS [Project Description], DUE.[CDTargetAmount] AS [Budget], MIN(WIP.[wdate]) [1st Date], CAST(SUM(WIP.[wRate]*WIP.[wHours]) AS decimal(34,2)) [Billable WIP], CAST( SUM(AR.[ARProgress])AS decimal(34,2)) [Progress], CAST((SUM(WIP.[wRate]*WIP.[wHours]) - SUM(AR.[ARProgress]))AS decimal(34,2)) [Net Wip], CAST(sum(Bucket.[cinvar])AS decimal(34,2)) [AR Balence], MAX(inv.[InvDate]) AS [Last InvoiceDate], due.[CDDateDelivered] AS [Project OTD Date]From [sab].[dbo].[WIP] AS WIP
Join [sab].[dbo].Clients AS CLT
ON WIP.[wcltID] = CLt.[ID]
Join [sab].[dbo].[cltdue] AS DUE
ON DUE.[CDID] = WIP.[wDue]
join [sab].[dbo].[DDEvents] AS E
ON DUE.[cdEventID] = e.[ID]
join [sab].[dbo].[Invoice] AS Inv
ON WIP.[wInvNum] = INV.[invNumber]
Join [sab].[dbo].[AcctsRec] AS AR
ON INV.[invNumber] = AR.[ARApplyTo]
Join [sab].[dbo].[ClientBuckets] AS Bucket
ON CLT.ID = Bucket.ID
JOIN [sab].[dbo].[cltBudget] AS Budget
ON clt.ID = cBudCltID
WHERE (@rptType = 'Audit - Payroll' AND e.[DDEventDesc] = 'Audit - Payroll' AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0) ) OR
(@rptType = 'Audits' AND (e.[ID] = '132' OR e.[ID] = '133' OR e.[ID] = '134' OR e.[ID] = '135' OR e.[ID] = '139' OR e.[ID] = '140' OR e.[ID] = '142' OR e.[ID] = '178')
AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)
) OR
(@rptType = 'Reviews & Comps' AND e.[DDEventDesc] IN ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc') AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)) OR
(@rptType = 'Assur Tax Returns' AND e.[DDEventDesc] IN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter') AND AR.[ARType] = 1 AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0) )
GROUP BY
CLT.[cltnum],
CLT.[clteng],
CLT.[cltName],
CLT.[CSPLname],
e.[DDEventDesc],
Budget.[CBudProjectDesc],
DUE.[CDTargetAmount],
DUE.[CDDateDelivered],
DUE.[CDDateReceived],
CLT.[CMGLname]
Having sum(Bucket.[cinvar])>0
'Audits' is the one that is taking very long.