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]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,
, [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
Post a Comment