Question : SQL 2005 Pivot Query Help!

Can anyone help with this?  I'm new to the pivot features on SQL 2005 queries -

SELECT            CONVERT(VARCHAR(7), DocData.DateCreated, 120) AS [Year/Month],
                  DocTypes.DocType AS [Document],
                  COUNT(DocData.JobId) AS Jobs
FROM            DocData INNER JOIN
                  DocTypes ON DocData.DocTypeId = DocTypes.DocTypeId
GROUP BY      CONVERT(VARCHAR(7), DocData.DateCreated, 120),
                  DocTypes.DocType


Trying to place the year/month in column 1, with Document accross the top row, with respective Job counts...

Thanks
Lapchien

Answer : SQL 2005 Pivot Query Help!

hi, try this
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
SELECT [Year/Month],[Document1],[Document2],[Document3],[Document4],[Document5]
FROM
(SELECT CONVERT(VARCHAR(7), DocData.DateCreated, 120) AS [Year/Month], DocTypes.DocType AS [Document], DocData.JobId
FROM            DocData INNER JOIN
                DocTypes ON DocData.DocTypeId = DocTypes.DocTypeId) D
PIVOT
(
COUNT(JobId)
FOR Document IN
([Document1],[Document2],[Document3],[Document4],[Document5])
) AS P
Open in New Window Select All
Random Solutions  
 
programming4us programming4us