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 375 times
-1
a_horse_with_no_name
- 497,550
- 91
- 775
- 843
irfan_db
- 9
- 5
-
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 – a_horse_with_no_name 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
- 139,860
- 19
- 198
- 228