1

我必须单独显示一个财政年度所有月份的费用报告。目前,我正在使用 SUM(Amount) 获取所有费用名称的特定月份的数据,但我想获取每个月的数据:-

例如:

Name    |    Amount    |     Month

Tea     |    3600      |      Apr
Tea     |    8500      |      May
Tea     |    3600      |      June
Tea     |    300       |      July
Tea     |    360       |      Aug
Tea     |    600       |      Sep
Tea     |    870       |      Oct

请帮助查询如何获取数据。谢谢

4

2 回答 2

0
SELECT Name, SUM(Amount) AS Amount, DATENAME(month, DateColumn) AS Month
FROM Table
GROUP BY Name, DATENAME(month, DateColumn)
于 2013-06-18T05:31:30.763 回答
-1
DECLARE @FinYearStart DATETIME

DECLARE @FinYearEnd DATETIME

SET @FinYearStart = DATEADD(dd,0, DATEDIFF(dd,0, DATEADD( mm, -(((12 + DATEPART(m, getDate())) - 4)%12), getDate() ) - datePart(d,DATEADD( mm, -(((12 + DATEPART(m, getDate())) - 4)%12),getDate() ))+1 ) )

SET @FinYearEnd = DATEADD(SS,-1,DATEADD(mm,12,@FinYearStart))

SELECT SUM(AMOUNT) AS Amount, 

DATENAME(month, DATECOLUMN) AS Month,

DATEPART(month, DATECOLUMN) AS Month1

FROM TABLENAME where DATECOLUMN between @FinYearStart

AND @FinYearEnd GROUP BY 

DATENAME(month, DATECOLUMN),DATEPART(month, DATECOLUMN)

order by Month1
于 2014-10-26T01:29:15.920 回答