我有这个查询可以提取月份和金额。像这样:
Aug 2015 61.87
Dec 2015 10.83
Feb 2015 80.00
Jul 2015 56.19
Jun 2015 31.65
Mar 2015 59.02
May 2015 46.91
Nov 2015 6.00
Oct 2015 8.27
Sep 2015 15.73
如您所见,订单不正确。这是我的查询
select
CONVERT(CHAR(4), InvoiceDate, 100) + CONVERT(CHAR(4), InvoiceDate, 120) as Month,
CONVERT(decimal(18,2), round(SUM(amount),2)) as Amount
from DataInvoice
where Vendor='IceCreamTruck'
and AccountNumber = '54554'
and InvoiceDate >= '02/01/2015'
and InvoiceDate <= '01/31/2016'
group by MONTH(InvoiceDate),
CONVERT(CHAR(4), InvoiceDate, 100) + CONVERT(CHAR(4), InvoiceDate, 120)
我尝试按Year()
,Month()
进行排序,但这不起作用。它抱怨ORDER BY
子句中的无效,因为它不包含在聚合函数或GROUP BY
子句中。
对此有何想法?顺便说一句发票是在YYYY-MM-DD格式中
感谢。
答案 0 :(得分:2)
您应该添加ORDER BY
子句:
ORDER BY RIGHT(CONVERT(CHAR(4), InvoiceDate, 100) + CONVERT(CHAR(4), InvoiceDate, 120),4)
MONTH(InvoiceDate)
这基本上按年度排序,然后按月份排序,所以它应该给你正确的排序。