PostgreSQL计数行分组

时间:2019-06-29 23:48:07

标签: sql postgresql group-by count

我在PostgreSQL数据库中有以下数据:

id | sub1 | sub2 | sub3 | sub4 | unique |
---|------|------|------|------|--------|
1  |  a   | null | null | null |  true  |
2  |  a   | null | null | null |  true  |
3  |  a   | null | null | null |  false |
4  |  a   |  b   | null | null |  true  |
5  |  a   |  b   | null | null |  false |
6  | null | null | null | null |  true  |

我需要这样的结果,例如,按子分组和计数:

 sub1 | sub2 | sub3 | sub4 | sum | unique_sum |
  a   | null | null | null |  3  |     2      |
  a   |  b   | null | null |  2  |     1      |
 null | null | null | null |  1  |     1      |

我对此查询感到困惑:

    SELECT   sub1, 
         sub2, 
         sub3, 
         sub4, 
         ( 
                SELECT Count(*) AS sum 
                FROM   data 
                WHERE 
                       CASE 
                              WHEN sub1 notnull THEN sub1 = d.sub1 
                              ELSE sub1 IS NULL 
                       END 
                AND 
                       CASE 
                              WHEN sub2 notnull THEN sub2 = d.sub2 
                              ELSE sub2 IS NULL 
                       END 
                AND 
                       CASE 
                              WHEN sub3 notnull THEN sub3 = d.sub3 
                              ELSE sub3 IS NULL 
                       END 
                AND 
                       CASE 
                              WHEN sub4 notnull THEN sub4 = d.sub4 
                              ELSE sub4 IS NULL 
                       END ) AS sum, 
         ( 
                SELECT count(*) AS unique_sum 
                FROM   data 
                WHERE  UNIQUE IS true 
                AND 
                       CASE 
                              WHEN sub1 notnull THEN sub1 = d.sub1 
                              ELSE sub1 IS NULL 
                       END 
                AND 
                       CASE 
                              WHEN sub2 notnull THEN sub2 = d.sub2 
                              ELSE sub2 IS NULL 
                       END 
                AND 
                       CASE 
                              WHEN sub3 notnull THEN sub3 = d.sub3 
                              ELSE sub3 IS NULL 
                       END 
                AND 
                       CASE 
                              WHEN sub4 notnull THEN sub4 = d.sub4 
                              ELSE sub4 IS NULL 
                       END ) AS unique_sum 
FROM     data                AS d 
ORDER BY sub1, 
         sub2, 
         sub3, 
         sub4

我更新了逻辑以获得更实际和详细的示例,添加了SUM和UNIQUE SUM计数。实际数据更加复杂。

1 个答案:

答案 0 :(得分:0)

这可以做到:

SELECT sub1, sub2, sub3, sub4
     , count(*) AS sum
     , count(unique_sum OR NULL) AS unique_sum
--   , count(*) FILTER (WHERE unique_sum) AS unique_sum  -- equivalent
FROM   test
GROUP  BY 1, 2, 3, 4
ORDER  BY 5 DESC, 6 DESC;  -- matches your demo result
-- ORDER BY 1, 2, 3, 4  -- or this, like in your query

相关: