i wants to show rows data into columns. suppose if rows type increased then number of columns also increased. Information as follow:
Asked
Active
Viewed 477 times
-1
-
See here: http://stackoverflow.com/questions/33513208/postgresql-query-with-dynamic-columns-and-counts-from-join and here: http://stackoverflow.com/questions/33223256/combining-multiple-rows-into-one and here: http://stackoverflow.com/questions/20618323/create-a-pivot-table-with-postgresql and here: http://stackoverflow.com/questions/31456734/dynamic-pivot-for-thousands-of-columns just to name a few – Nov 09 '15 at 13:15
1 Answers
0
You can use COUNT
with CASE WHEN
:
SELECT t.Name AS Type,
COUNT(*) AS NumberOfCase,
COUNT(CASE WHEN s.Name = 'Resolved' THEN 1 END) AS Resolved,
COUNT(CASE WHEN s.Name = 'Pending' THEN 1 END) AS Pending,
COUNT(CASE WHEN s.Name = 'Waiting' THEN 1 END) AS Waiting
FROM Type t
LEFT JOIN "Case" c
ON c.CaseType = t.TypeId
LEFT JOIN "Status" s
ON c.CaseStatus = s.StatusId
GROUP BY t.Name;

Lukasz Szozda
- 162,964
- 23
- 234
- 275