sql group by查询与SUM求和
表:x_IDx_salex_dateA2002009-12-01B3002009-12-01A1002009-12-02C702009-12-02B502009-12-0...
表:
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)要怎么体现出来,怎么写呢?? 展开
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)要怎么体现出来,怎么写呢?? 展开
展开全部
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&"')
&"' 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&"')
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select 订单ID ,sum(金额) from 订单表 --假设以订单ID汇总,订单ID和金额都是订单表里的字段
group by 订单ID
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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……
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……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select x_id,sum(x_sale)
from 表名
where x_date between to_date('20091201','yyyymmdd') and to_date('20091203','yyyymmdd')
group by x_id
from 表名
where x_date between to_date('20091201','yyyymmdd') and to_date('20091203','yyyymmdd')
group by x_id
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询