SQL Server中所有会计年度(4月至3月)的财务年度报表

时间:2015-04-23 10:45:15

标签: asp.net sql-server tsql stored-procedures pivot-table

我在哪里可以使用T-sql动态生成过去10年的财务年度报告:

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

1 个答案:

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

这应该让你接近获得每个财政年度的费用。要获得平均值,只需执行相同操作并将其聚合即可。