简体   繁体   English

获取SQL表列中唯一组合的数量

[英]getting the count of unique combinations in columns of a sql table

Assuming you have a database with patrons table with the following columns and 100,000 rows: 假设您有一个带有patrons表的数据库,该表包含以下列和100,000行:

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

A few example rows are the following: 以下是一些示例行:

+----+------+-------+-------+-------+
| 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   |
+----+------+-------+-------+-------+

And I want a table like so (A = department A only, B = department B only , C = department C only, AB = department A and B, etc) : 我想要一个这样的表(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 |
+-----------+------------+

A person would be considered to be not part of a department if the value for a department is NULL. 如果部门的值为NULL,则将认为该人不属于部门。 For example, if I was only part of department A, the value of department A would be my id value, and the values for department B and C would be NULL. 例如,如果我只是部门A的一部分,部门A的值将是我的id值,部门B和C的值将为NULL。

What would be the query in SQL that does this? SQL中执行此操作的查询将是什么? I'm lost on how to find the unique (distinct) combinations and make them their own column. 我不知道如何找到独特的(独特的)组合并使它们成为自己的专栏。

You should do this with a single aggregation: 您应该使用单个聚合来执行此操作:

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;
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
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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM