按年显示总销售额。显示年份和总销售额。按年

时间:2016-04-30 04:05:13

标签: mysql sql

我有以下问题要回答作业。这是我的最后一个问题,我完全迷失了。问题是:

按年显示总销售额。显示年份和总销售额。按年份排序。

我必须继续进行的ERD图像链接如下:

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

如何将行显示为仅显示一年,每年正确添加的总数,并仅显示年份?

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您应该使用year函数提取日期年份,如下所示:

SELECT YEAR(SalesOrder.OrderDate) AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY YEAR(SalesOrder.OrderDate);

然后,您的查询将按照您的预期运作。

答案 1 :(得分:0)

SELECT YEAR(SalesOrder.OrderDate) AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY year(SalesOrder.OrderDate);

答案 2 :(得分:0)

使用年份功能按日期分组。

SELECT YEAR(SalesOrder.OrderDate) AS year,
SUM(SalesOrder.OrderTotal) AS OrderTotal
FROM
SalesOrder
GROUP BY YEAR(SalesOrder.OrderDate);