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
|