我有以下问题需要回答作业。这是我最后一个问题,我完全迷失了。问题是:
按年份显示总销售额。显示年份和总销售额。按年份排序。
我必须继续进行的 ERD 图像链接如下:
这是我到目前为止所拥有的:
SELECT SalesOrder.OrderDate AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY SalesOrder.OrderDate;
打印出以下内容:
year OrderTotal
2/3/2011 5:37:11 PM 372.0000
3/17/2011 3:33:00 PM 268.0000
3/19/2011 10:33:00 AM 21782.5000
3/7/2012 12:00:00 AM 871.5300
4/6/2012 12:00:00 AM 1010.0000
4/9/2012 6:47:39 PM 540.9000
4/10/2012 9:40:00 PM 22.8300
5/7/2013 11:42:37 AM 11.8500
5/7/2013 7:36:05 PM 35.6000
这应该是我的结果:
year OrderTotal
2011 22422.5000
2012 2445.2600
2013 47.4500
如何让行只显示一年、每年正确添加的总计,并且只显示年份?
提前感谢您的帮助。
SELECT YEAR(SalesOrder.OrderDate) AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY year(SalesOrder.OrderDate);
使用“年份”功能按日期的年份进行分组。
SELECT YEAR(SalesOrder.OrderDate) AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY YEAR(SalesOrder.OrderDate);
您应该使用
year
函数提取日期的年份,如下所示:
SELECT YEAR(SalesOrder.OrderDate) AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY YEAR(SalesOrder.OrderDate);
然后,您的查询将按您的预期运行。
选择年份(S.OrderDate)作为年份, SUM(S.OrderTotal) AS Total_Sales 从 销售订单S 按年份分组(S.OrderDate) 按年份 ASC 订购