Question

I need this select query to return the sum of the same column based on different where clauses. Basically just looking for a way to merge these multiple select queries. Please see below. Any help is very much appreciated!

DECLARE @prEndDate  smalldatetime
SET @prEndDate='2014-01-05'

SELECT  Employee, SUM(Amount) as Deduction
FROM bPRDT
WHERE PREndDate=@prEndDate  
AND EDLCode=100 AND EDLType='D'  
GROUP BY Employee

SELECT  Employee, SUM(Amount) as DeductionPlus
FROM bPRDT
WHERE PREndDate=@prEndDate  
AND EDLCode=101 AND EDLType='D'  
GROUP BY Employee

SELECT  Employee, SUM(Amount) as Match
FROM bPRDT
WHERE PREndDate=@prEndDate  
AND EDLCode=600 AND EDLType='L'  
GROUP BY Employee

SELECT  Employee, SUM(Amount) as MatchPlus
FROM bPRDT
WHERE PREndDate=@prEndDate  
AND EDLCode=601 AND EDLType='L'  
GROUP BY Employee

Finally fixed. Here's what it ended up being:

DECLARE @prEndDate smalldatetime
SET @prEndDate ='2013-12-29'
SELECT  
REPLACE(c.SSN,'-',''),
SUM(CASE WHEN a.EDLType='D' AND a.Amount > 0 AND (a.EDLCode=100 OR a.EDLCode=101) THEN a.Amount ELSE 0 END) AS Deferral,
SUM(CASE WHEN a.EDLType='L' AND a.Amount > 0 AND (a.EDLCode=600 OR a.EDLCode=601) THEN a.Amount ELSE 0 END) AS EmployerMatch,
SUM(CASE WHEN a.EDLType='D' AND a.Amount > 0 AND (a.EDLCode=100 OR a.EDLCode=101) THEN a.SubjectAmt ELSE 0 END) AS Compensation415,
SUM(CASE WHEN a.EDLType='D' AND a.Amount > 0  AND (a.EDLCode=100 OR a.EDLCode=101) THEN a.SubjectAmt ELSE 0 END) AS PlanFullYearCompensation,
SUM(CASE WHEN a.EDLType='E' AND a.Amount > 0 AND (a.EDLCode=1 OR a.EDLCode=2 OR a.EDLCode=3)  THEN a.Hours ELSE 0 END) AS PlanHours
FROM  bPRDT a  
JOIN (SELECT DISTINCT SSN, Employee FROM bPREH) c ON a.Employee=c.Employee    
WHERE a.PREndDate=@prEndDate 

GROUP BY c.SSN  
ORDER BY Deferral DESC, c.SSN ASC  
Was it helpful?

Solution

You could use UNION or CASE WHEN clauses.

SELECT  
      Employee, 
      SUM(CASE WHEN EDLCode=100 AND EDLType='D' THEN Amount ELSE 0 END) as Deduction,
      SUM(CASE WHEN EDLCode=101 AND EDLType='D' THEN Amount ELSE 0 END) as DeductionPlus
      SUM(CASE WHEN EDLCode=600 AND EDLType='L' THEN Amount ELSE 0 END) as Match
      SUM(CASE WHEN EDLCode=601 AND EDLType='L' THEN Amount ELSE 0 END) as MatchPlus
FROM  bPRDT
WHERE PREndDate=@prEndDate 
GROUP BY Employee

OTHER TIPS

You can move the condition out of the WHERE clause and in to a CASE statement, so you only sum the rows that interest you:

DECLARE @prEndDate  smalldatetime
SET @prEndDate='2014-01-05'

SELECT   Employee, 
         SUM(CASE WHEN EDLCode=100 AND EDLType='D' THEN amount ELSE 0 END) 
           AS Deduction,
         SUM(CASE WHEN EDLCode=101 AND EDLType='D' THEN amount ELSE 0 END)
           AS DeductionPlus,
         SUM(CASE WHEN EDLCode=600 AND EDLType='L' THEN amount ELSE 0 END)
           AS Match,
         SUM(CASE WHEN EDLCode=601 AND EDLType='L' THEN amount ELSE 0 END)
           AS MatchPlus
FROM     bPRDT
WHERE    PREndDate = @prEndDate  
GROUP BY Employee

Group by Employee,EDLCode,EDLType instead of just Employee.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top