sql group by查询与SUM求和

表:
x_ID x_sale x_date
A 200 2009-12-01
B 300 2009-12-01
A 100 2009-12-02
C 70 2009-12-02
B 50 2009-12-03
A 150 2009-12-03
要求查询01号到03号A的总x_sale,B的总x_sale,C的总x_sale,还有01号到03号所有的总和,正确的sql要怎么写,
我现在用group by 求出分开的总数,但所有的总数老是出错,
Select TABLE1.x_ID,SUM(TABLE1.x_sale) AS sumsalein FROM WHERE (TABLE1.x_date BETWEEN '"&2009-12-01
&"' AND '"&2009-12-03&"') GROUP BY TABLE1.x_ID,
这样可以求出A的总x_sale,B的总x_sale,C的总x_sale,但是还是一个总数SUM(A+B+C)要怎么体现出来,怎么写呢??

第1个回答  推荐于2017-09-12
Select TABLE1.x_ID,SUM(TABLE1.x_sale) AS sumsalein FROM table1 WHERE (TABLE1.x_date BETWEEN '"&2009-12-01
&"' AND '"&2009-12-03&"') GROUP BY TABLE1.x_ID
union
select '总和',sum(table1.x_sale) as sumsalein from table1 where (TABLE1.x_date BETWEEN '"&2009-12-01
&"' AND '"&2009-12-03&"')本回答被提问者采纳
第2个回答  2015-10-31
select 订单ID ,sum(金额)  from 订单表  --假设以订单ID汇总,订单ID和金额都是订单表里的字段 
group by 订单ID

第3个回答  2009-12-13
select
sum(case when x_id='A' then x_sale End) As SumA,
sum(case when x_id='B' then x_sale End) As SumB,
sum(case when x_id='C' then x_sale End) As SumC,
sum( x_sale ) As SumAll
From Where……
第4个回答  2009-12-13
select x_id,sum(x_sale)
from 表名
where x_date between to_date('20091201','yyyymmdd') and to_date('20091203','yyyymmdd')
group by x_id
相似回答