Quantcast
Channel: Answers for "Distinct sums of case statements"
Viewing all articles
Browse latest Browse all 6

Answer by Beandon10

0
0
If you don't need the separate dates in columns, you could write the query as: DECLARE @Emp TABLE ( EmpID INT, hist_date DATETIME ) INSERT @Emp(EmpID, hist_date) VALUES (132, '7/1/2012'), (143, '7/1/2012'), (154, '7/2/2012'), (154, '7/3/2012'), (123, '8/1/2012'), (197, '8/1/2012'), (197, '8/2/2012'), (187, '8/3/2012'), (123, '9/1/2012'), (197, '9/1/2012'), (197, '9/2/2012'), (187, '9/3/2012'); SELECT * FROM (SELECT DISTINCT DATENAME(MONTH, hist_date) + CONVERT(VARCHAR(10), DATEPART(YEAR, hist_date)) AS Dt, EmpID FROM @Emp) AS Src PIVOT ( COUNT(EmpID) FOR Dt IN ([July2012], [August2012], [September2012]) ) AS pvt

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images