我有这样的主表。
[![在此处输入图片描述] [1]] [1]
输出为
[![在此处输入图片描述] [2]] [2]
我该怎么做才能得到这样的输出?
答案 0 :(得分:0)
您可以尝试像使用GROUP BY
和UNION ALL
一样。
select count(*) CT,Mark
from TableName
group by Mark
union all
select Count(*), 'A+B' as mark
from TableName
where mark in('A','B')
UNION ALL
select Count(*), 'A+C' as mark
from TableName
where mark in('A','C')
union all
select Count(*), 'B+C' as mark
from TableName
where mark in('B','C')
UNION ALL
select Count(*), 'A+B+C' as mark
from TableName
答案 1 :(得分:0)
执行cross join
和union all
select mark,count(*) as cnt
from cte group by mark
union all
select concat(a.mark,'+',b.mark) as mark,a.cnt+b.cnt as cnt from
(select mark,count(*) as cnt
from cte group by mark
) a cross join
(
select mark,count(*) as cnt
from cte group by mark
) b where a.mark<b.mark
union all
select 'A+B+C' ,sum(cnt)
from
(
select mark,count(*) as cnt
from cte group by mark
) a
输出
mark cnt
A 1
B 1
C 2
A+B 2
A+C 3
B+C 3
A+B+C 4