
sql 表aaa有若干字段a,b,c,只用一条sql语句,完成统计分别a=1,2,3;b=1,2,3;c=1,2,3
1个回答
展开全部
可以参考下列SQL语句,一次性输出3个字段的各自的合计数,只汇总那3个字段的值分别为1,2,3的记录
select sum(t.a) as sumA,sum(t.b) as sumB,
sum(t.c) as sumC from
(select a,0 as b, 0 as c from aaa where a in (1,2,3)
union all
select 0,b, 0 from aaa where b in (1,2,3)
union all
select 0,0, c from aaa where c in (1,2,3)) t
select sum(t.a) as sumA,sum(t.b) as sumB,
sum(t.c) as sumC from
(select a,0 as b, 0 as c from aaa where a in (1,2,3)
union all
select 0,b, 0 from aaa where b in (1,2,3)
union all
select 0,0, c from aaa where c in (1,2,3)) t
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询