Dayanand Jawalkar Dayanand Jawalkar - 2 months ago 10
SQL Question

show data in group by formate

Name Status
A Fail
B open
c fail
A Success
A open
A Success


I want Output like,

Name S_open S_Success S_All
A 1 2 4
B 1 0 1
C 0 0 1

Answer

Try like this

SELECT Name,
   SUM(CASE WHEN Status='open' THEN 1 END) AS S_open,
   SUM(CASE WHEN Status='Success' THEN 1 END) AS S_Success,
   SUM(1) AS S_All
FROM Table
GROUP BY Name;