A B C
1 1 5
1 1 3
1 2 4
1 2 7
用怎样的SQL语句得到结果:
A X Y Z
1 8 11 19
字段B为1的,把字段C值相加,B为2的C值相加,然后在一行查出来?
參考答案:select a,x,y,z from table,
(select a,sum(c) as x from table
where b=1
group by a,b) table_1,
(select a,sum(c) as y from table
where b=2
group by a) table_2,
(select a,sum(c) as z from table
group by a,b) table_3
where table.a=table_1.a
and table.a=table_2.a
and table.a=table_3.a