我正在尝试根据我的数据库制作摘要报告。它必须按位置逐年分组,我想要总计该分组中的订单数量。
需要看起来像:
Year Week Location Total Amount
2014 1 Atlanta 22,000
2014 1 Schaumberg 32,566
2014 1 Dallas 32,567
2014 1 New York 32,356
2014 2 Atlanta 22,000
2014 2 Schaumberg 32,566
2014 2 Dallas 32,567
2014 2 New York 32,356
我的表(system_order)结构设置如下:
Order Amount Location Week Year
1 1895 Schaumberg 1 2014
2 1295 Atlanta 1 2014
3 1895 Atlanta 1 2014
4 1895 New York 1 2014
5 1495 Dallas 2 2014
6 1695 Schaumberg 2 2014
7 1895 Schaumberg 2 2014
8 1895 Dallas 2 2014
9 1895 New York 2 2014
可以在一个sql语句中完成吗?
答案 0 :(得分:3)
SELECT Year, Week, Location, sum(Amount) as 'Total Amount'
FROM [system_order]
GROUP BY Location, Week, Year
答案 1 :(得分:1)
我没有测试。这应该是公共汽车。
select sum(amount), year,week,location
from (system_order)
group by Location, week, year
order by sum(amount)
答案 2 :(得分:1)
通常,您必须将所选行放入group by子句中。例外情况是它们是否在select子句中的聚合函数中。
这就应该有效。
Select Year
,Week
,Location
,sum(amount) as 'Total_Amount'
from (system_order)
group by Year
,Week
,Location