将行数据显示为列智慧

时间:2015-11-09 12:54:00

标签: postgresql pivot postgresql-9.1 postgresql-9.3 crosstab

我想将行数据显示到列中。假设如果行类型增加,那么列数也会增加。信息如下:

Please see picture to get idea

1 个答案:

答案 0 :(得分:0)

您可以将COUNTCASE 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