pivot in sql server 2008

PIVOT in SQL Server

PIVOT() is used to convert  the rows to columns in SQL Server. 


The example as give below. 

SELECT ISNULL(PVT.JANUARY,'0') AS JANUARY,ISNULL(PVT.FEBRUARY,'0')FEBRUARY,ISNULL(PVT.MARCH,'0')MARCH,
ISNULL(PVT.APRIL,'0')APRIL,ISNULL(PVT.MAY,'0')MAY,ISNULL(PVT.JUNE,'0')JUNE,ISNULL(PVT.JULY,'0')JULY,
ISNULL(PVT.AUGUST,'0')AUGUST,ISNULL(PVT.SEPTEMBER,'0')SEPTEMBER,ISNULL(PVT.OCTOBER,'0') OCTOBER,
ISNULL(PVT.NOVEMBER,'0')NOVEMBER,ISNULL(PVT.DECEMBER,'0') DECEMBER
FROM(SELECT DATENAME(MONTH,CREATEDDATE) AS [MONTH],COUNT(*) AS CCOUNT FROM COMPANY
WHERE TENANTID=1 AND ISNULL(ISDELETED, 0) <> 1
GROUP BY   DATENAME(MONTH,CREATEDDATE)
) AS TMP
PIVOT(SUM(CCOUNT) FOR [MONTH]
IN(JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER)) AS PVT  
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^