字符串拆分功能的SQL分组/计数

时间:2019-08-15 16:44:44

标签: sql legacy-sql

好吧,我原来是这个

select people, count(*) 
from table
group by people

但是有些人有多个人,所以这种聚合不会为您提供A,B,C的纯计数,而是每次迭代都是

A 10
B 5
A, B 1
A, C 2
C 15
A, B, C 3

这可以获取遗留sql中个人的完整列表

select split(people,",") as person
from table

但是我不能在其上使用分组方式

select split(people,",") as person, count(*)
from table
group by person

给出错误

  

无法按汇总分组。

我觉得解决方案是某种程度上的子查询,但是我不确定如何执行

1 个答案:

答案 0 :(得分:1)

尝试使用外部查询包装

select person, count(*)
from(
    select split(people,",") as person
    from table
) t
group by person