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
I'm trying to PIVOT this result as below
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
I'm trying to PIVOT this result as below
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
would be
with base as
(
SELECT Project
,STATUS
,count(*) AS [count]
FROM Defect
GROUP BY Project
,STATUS
)
select *
from base
pivot (
sum([count])
for base.status in ([Build Delivered],[New],[...],[...])
) as x
order by project
;
You can use aggregated function sum
with case
expressions as following
select
project,
sum(case when status = 'Build Assigned' then count else 0 end) as Build_Assigned,
sum(case when status = 'Build Delivered' then count else 0 end) as Build_Delivered
from yourTable
group by
project