Skip to content
Advertisement

How to PIVOT a select result as mentioned in the query

I have query as mentioned below

SELECT Project
    ,STATUS
    ,count(*) AS [count]
FROM Defect
GROUP BY Project
    ,STATUS
ORDER BY Project

which will return a table like

enter image description here

I’m trying to PIVOT this result as below

enter image description here

Advertisement

Answer

You can try the below way –

SELECT * FROM   
(
    SELECT Project,STATUS,count(*) AS cnt FROM Defect GROUP BY Project,STATUS
) t 
PIVOT(
    sum(cnt) 
    FOR STATUS IN (
        [Build Assigned], 
        [Build Delivered], 
        [Closed], 
        [Eng. Build Delivered], 
        [New], 
        [Non-Issue], 
        [Open],[Pending])
) AS pivot_table
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement