-1

i wants to show rows data into columns. suppose if rows type increased then number of columns also increased. Information as follow:

Please see picture to get idea

  • 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 Answers1

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;

SqlFiddleDemo

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