SQL server T-SQL: converting rows into columns example : Pivot example

In one of our requirement we need  to convert the rows into columns in sql server for that
we used pivot keyword .

here is the example :

we need get the number of counts for each status on date basis .


SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, getdate()-1)) AS theDate,
[Active]
, [On-Order], [Scrapped], [Stock]FROM(SELECT SIMStatus, simid
FROM tbl_sims) AS SourceTablePIVOT
(
count(simid)FOR SIMStatus IN ([Active], [On-Order], [Scrapped], [Stock]))
AS PivotTable;

Comments

Popular posts from this blog

SSAS : A connection cannot be made to redirector. Ensure that 'SQL Browser' service is running. (Microsoft.AnalysisServices.AdomdClient). + Solution

SSIS Error : Unable to infer the XSD from the XML file. The XML contains multiple namespaces