2011-11-01 86 views
0

我有一個sql查詢,顯示一列中的年份和另一列中的月份。我可以將報告顯示爲當年,但我需要以財政形式顯示,例如我們的財政年度是從十月十日開始的,所以我如何在sql中執行此操作?我下面當前查詢:報告中的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

這是我要做的事:

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

因爲我們在十一月,這將返回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