2015-04-23 73 views
0

我在哪裏可以動態生成像這樣使用T-SQL的近10年財政年度報告:財政年度語句所有財務年度(4月至三月)

Months |2007-2008 |2008-2009| 2009-2010 |2010-2011 | Average 
------------------------------------------------------------------- 
April      
May         
June          
July          
August         
September         
October         
November          
December          
January         
February          
March         
Average  
+4

我們需要您的表格結構和示例數據,以獲得任何幫助 – Raj

+0

ID,Year,Month,Expense,Datetime,monthorder其中我的SQL表列 – Prhem

回答

0

我不知道[DATETIME]代表什麼,我假設它是月份或年份的指標,如果不是,您需要更改where語句以結合YEAR和MONTH。

嘗試這樣:

SELECT '2007-2008' AS FISCALYEAR, SUM(EXPENSE) AS EXPENSE 
FROM YOURTABLE 
WHERE [DATETIME] BETWEEN '4/1/2007' AND '3/31/2008' 
UNION ALL 
SELECT '2008-2009' AS FISCALYEAR, SUM(EXPENSE) AS EXPENSE 
FROM YOURTABLE 
WHERE [DATETIME] BETWEEN '4/1/2008' AND '3/31/2009' 
UNION ALL 
SELECT '2009-2010' AS FISCALYEAR, SUM(EXPENSE) AS EXPENSE 
FROM YOURTABLE 
WHERE [DATETIME] BETWEEN '4/1/2009' AND '3/31/2010' 
UNION ALL 
SELECT '2010-2011' AS FISCALYEAR, SUM(EXPENSE) AS EXPENSE 
FROM YOURTABLE 
WHERE [DATETIME] BETWEEN '4/1/2010' AND '3/31/2011' 

這應該讓你接近獲得費用每個會計年度。爲了獲得平均值,只需執行相同的操作並彙總即可。

相關問題