查询在一个财政年度的月份之间获取数据

时间:2013-06-18 05:21:24

标签: sql-server

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

例如:

Name    |    Amount    |     Month

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

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

2 个答案:

答案 0 :(得分:0)

SELECT Name, SUM(Amount) AS Amount, DATENAME(month, DateColumn) AS Month
FROM Table
GROUP BY Name, DATENAME(month, DateColumn)

答案 1 :(得分:-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