0

我试图找出一个查询来说明今天什么时候属于这个财政年度(1/11 - 31/10),运行这组案例语句,但是当今天是下一个财政年度时运行下一组等等。 .

我试过嵌套 CASE 语句但没有工作。下面只是解释我想要做什么..

感谢您的关注!

IF Getdate() Between '2012-11-01 00:00' AND '2013-10-31 23:59' -- is it this year?

THEN
        CASE
         WHEN B.saledate Between '2011-11-01 00:00' AND Getdate()-365 THEN 'Yes'
         WHEN B.saledate Between '2012-11-01 00:00' AND Getdate() THEN 'Yes'
        Else 'No'
        AS 'Financial year to date',
        END

IF Getdate() Between '2013-11-01 00:00' AND '2014-10-31 23:59' -- is it this year yet?
THEN
        CASE
         WHEN B.Created Between '2011-11-01 00:00' AND Getdate()-730 THEN 'Yes'
         WHEN B.Created Between '2012-11-01 00:00' AND Getdate()-365 THEN 'Yes'
         WHEN B.Created Between '2013-11-01 00:00' AND Getdate() THEN 'Yes'
        Else 'No'
        AS 'Financial year to date',
        END

END
4

2 回答 2

0

这样,您最终会得到不断增加的代码。

要获得某个财政年度,oyu 可以尝试:

DECLARE @DateToCheck AS DATETIME

SET @DateToCheck = '20130101'

SELECT YEAR(DATEADD(mm,-10,@DateToCheck))

因此,如果您有一个Transactions带有 DateTime 字段的表TransactionDate

SELECT YEAR(DATEADD(mm,-10,TransactionDate))
FROM   Transactions

将为您提供每笔交易的会计年度。

于 2013-09-04T08:51:45.020 回答
0

感谢您的帮助,但这是迄今为止这个财政年度的关键,而不仅仅是财政年度。

按以下方式解决。这确实意味着每年都会添加一个新块,但这就是我现在所处的位置。

CASE
         WHEN 
         Getdate() Between '2012-11-01 00:00' AND '2013-10-31 23:59' AND
         (
         cast(convert(char(11), B.Created, 10) as datetime) Between '2009-11-01 00:00' AND (cast(getdate()-1096 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2010-11-01 00:00' AND (cast(getdate()-731 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2011-11-01 00:00' AND (cast(getdate()-366 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2012-11-01 00:00' AND (cast(getdate() as date))
         )
         THEN 'Yes' 

         WHEN Getdate() Between '2013-11-01 00:00' AND '2014-10-31 23:59' AND
         (
         cast(convert(char(11), B.Created, 10) as datetime) Between '2009-11-01 00:00' AND (cast(getdate()-1461 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2010-11-01 00:00' AND (cast(getdate()-1096 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2011-11-01 00:00' AND (cast(getdate()-731 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2012-11-01 00:00' AND (cast(getdate()-366 as date)) OR
         cast(convert(char(11), B.Created, 10) as datetime) Between '2013-11-01 00:00' AND (cast(getdate() as date))
         )
         THEN 'Yes'

Else 'No'
END
AS 'Financial year to date',
于 2013-09-04T12:48:29.003 回答