在sql表的列中获取唯一组合的计数

时间:2017-03-03 01:08:23

标签: sql database postgresql

假设您的数据库包含以下列和100,000行的顾客表:

CREATE TABLE patron (
  id       INT,
  name     VARCHAR(100),
  deptA    INT,
  deptB    INT,
  deptC    INT
 );

以下是一些示例行:

+----+------+-------+-------+-------+
| id | name | deptA | deptB | deptC |
+----+------+-------+-------+-------+
| 1  | Bob  |  1    |   1   | NULL  |
| 2  | Bill | NULL  |   2   | NULL  |
| 3  | Mike |  3    | NULL  | NULL  |
| 4  | John |  4    |   4   |   4   |
| 5  | Matt | NULL  | NULL  |   5   |
| 6  | Jack |  6    |   6   | NULL  |
| 7  | Sean |  7    |   7   |   7   |
| 8  | Adam |  8    |   8   |   8   |
+----+------+-------+-------+-------+

我想要一张这样的桌子(A =仅A部门,B =仅B部门,C =仅C部门,AB =部门A和B等):

+-----------+------------+
| Department| User Count |
+-----------+------------+
| A         |     40,121 |
| B         |     25,663 |
| C         |     13,925 |
| AB        |      6,253 |
| AC        |      5,870 |
| BC        |      5,123 |
| ABC       |      3,045 |
+-----------+------------+

如果某个部门的值为NULL,则该人员将被视为不属于某个部门。例如,如果我只是部门A的一部分,则部门A的值将是我的id值,部门B和C的值将为NULL。

在SQL中执行此操作的查询是什么?我迷失在如何找到独特(不同)的组合并使它们成为自己的专栏。

3 个答案:

答案 0 :(得分:2)

您应该使用单个聚合执行此操作:

select concat_ws(',',
                 (case when deptA > 0 then 'A' end),
                 (case when deptB > 0 then 'B' end),
                 (case when deptC > 0 then 'C' end)
                ) as Department
        count(*)
from patron
group by Department
order by length(Department), Department;

答案 1 :(得分:0)

select 'A' Department, count(id) UserCount from patron where deptA = id
union all
select 'B' Department, count(id) UserCount from patron where deptB = id
union all
select 'C' Department, count(id) UserCount from patron where deptC = id
union all
select 'AB' Department, count(id) UserCount from patron where deptA = id and deptB = id
union all
select 'AC' Department, count(id) UserCount from patron where deptA = id and deptC = id
union all
select 'BC' Department, count(id) UserCount from patron where deptB = id and deptC = id
union all
select 'ABC' Department, count(id) UserCount from patron where deptA = id and deptB = id and deptC = id

答案 2 :(得分:0)

select "A" as Department, 
 (select count(1) from patron 
  where deptA is not null 
  and deptB is null 
  and deptC is null) as "User Count"
union all
select "B" as Department, 
 (select count(1) from patron 
  where deptA is null 
  and deptB is not null 
  and deptC is null) as "User Count"
union all
select "C" as Department, 
 (select count(1) from patron 
  where deptA is null 
  and deptB is null 
  and deptC is not null) as "User Count"
union all
select "AB" as Department, 
 (select count(1) from patron 
  where deptA is not null 
  and deptB is not null 
  and deptC is null) as "User Count"
union all
select "AC" as Department, 
 (select count(1) from patron 
  where deptA is not null 
  and deptB is null 
  and deptC is not null) as "User Count"
union all
select "BC" as Department, 
 (select count(1) from patron 
  where deptA is null 
  and deptB is not null 
  and deptC is not null) as "User Count"
union all
select "ABC" as Department, 
 (select count(1) from patron 
  where deptA is not null 
  and deptB is not null 
  and deptC is not null) as "User Count"