报告中的SQL会计年度

时间:2011-11-01 17:11:47

标签: sql

我有一个SQL查询,它在一列中显示年份,在另一列中显示月份。我可以将报告显示为当前年份,但我需要以财务形式显示它,例如我们的财政年度是从oct-oct开始,那么我怎样才能在sql中执行此操作?我目前的查询如下:

SELECT TOP (15) dbo.timelinedayres_v1_0.fiscalyear, 
                dbo.timelinedayres_v1_0.fiscalmonth, 
                SUM(dbo.factqht_v2.baanstandarcost)                 AS stdcost, 
                CAST(dbo.timelinedayres_v1_0.fiscalmonth AS VARCHAR) + ' - ' + 
                CAST(dbo.timelinedayres_v1_0.fiscalyear AS VARCHAR) AS CONCAT 
FROM   dbo.timelinedayres_v1_0 
       INNER JOIN dbo.factqht_v2 
         ON dbo.timelinedayres_v1_0.epoch = dbo.factqht_v2.qhtwfclosedtimekey 
       INNER JOIN dbo.dimqht_v2 
         ON dbo.factqht_v2.qhtid = dbo.dimqht_v2.qhtid 
       LEFT OUTER JOIN dbo.dimqhtmaterials_v2 
         ON dbo.factqht_v2.materialid = dbo.dimqhtmaterials_v2.materialid 
       LEFT OUTER JOIN dbo.dimqhtdispositions_v2 
         ON dbo.factqht_v2.dispositionid = 
            dbo.dimqhtdispositions_v2.dispositionid 
GROUP  BY dbo.dimqhtdispositions_v2.TYPE, 
          dbo.timelinedayres_v1_0.fiscalyear, 
          dbo.timelinedayres_v1_0.fiscalmonth, 
          dbo.dimqht_v2.toplevelassembly_majoritemgroupdesc, 
          CAST(dbo.timelinedayres_v1_0.fiscalmonth AS VARCHAR) + ' - ' + 
          CAST(dbo.timelinedayres_v1_0.fiscalyear AS VARCHAR) 
HAVING ( dbo.dimqhtdispositions_v2.TYPE = N'Scrap' ) 
       AND ( dbo.dimqht_v2.toplevelassembly_majoritemgroupdesc = 
             'Torquer Motors' ) 
ORDER  BY dbo.timelinedayres_v1_0.fiscalmonth  

1 个答案:

答案 0 :(得分:1)

我就是这样做的:

SELECT YEAR(DATEADD(month, 2 + DATEDIFF(month, 0, GETDATE()), 0))

由于我们在11月,这将返回2012年,如果日期是10月,它将返回2011年。您可以调整您的偏移量。

如果你从一个月/一年进入,那么你可以这样做:

declare @month int
declare @year int
set @year = 2011
set @month = 10
    SELECT CASE  
               WHEN @month > 10  
                   THEN @year + 1
               ELSE 
                   @year
           END AS fiscalYear