[SQL]不同字段同一内容的值进行count统计

img


请问我该如何对不同字段,同一内容的值进行count统计
变成如下这种样子

img

create table test_20220628(a varchar(200),b varchar(200),c varchar(200),d varchar(200));
insert into test_20220628 values ('五险一金','五险一金','下午茶','五险一金');
insert into test_20220628 values ('年终奖金','五险一金','下午茶','五险一金');

create table test_20220628_2(name varchar(200));
insert into test_20220628_2 values ('五险一金');
insert into test_20220628_2 values ('下午茶');
insert into test_20220628_2 values ('年终奖金');

SELECT k.name,
( LENGTH( xx) - LENGTH(REPLACE( xx,k.name,'' )))/length(k.name) AS num 
FROM (select group_concat(concat(a,',',b,',',c,',',d)) xx from test_20220628) x,test_20220628_2 k 

img

只能分两步,先做子查询的union all,再统计

有帮助请采纳,还有不懂的来我技术群问~

select count(1), t.weal from (
    select weal1 weal from [tableName] union all
    select weal2 weal from [tableName] union all
    select weal3 weal from [tableName] union all
    // ...此处省略多列的查询
    select wealn weal from [tableName]
) t 
group by t.weal asc

其中,[tableName] 替换成为你实际开发中的表名,如果有空间(模式)名称,需要增加空间(模式)名称