在我的子查询中,我在Select stmt中有这一行来执行DATEDIFF:
DATEDIFF(day,[Invoice_Date], '2015-02-27 00:00:00.000') AS Days_AGED,
在我使用该INT或Number返回的主要查询中,我在我的select stmt中有以下代码进行测试:
(Days_AGED + 5) AS TEST_AGE, --This worked and gave me the correct number when 5 was added to the variable Days_AGED
CASE
WHEN (CAST(Days_AGED AS NUMERIC(10,0)) >= CAST(30 AS NUMERIC(10,0))) THEN CAST(Days_AGED AS VARCHAR(5)) + ' 0-30 Days' -- This worked when the number was 30 or a large number
WHEN (Days_AGED >= 31 AND Days_AGED <= 60) THEN '31-60 Days'
WHEN (Days_AGED >= 61 AND Days_AGED <= 90) THEN '61-90 Days'
WHEN (Days_AGED >= 91 AND Days_AGED <= 120) THEN '91-120 Days'
WHEN (Days_AGED >= 121 AND Days_AGED <= 150) THEN '121-150 Days'
ELSE '151+ Days'
END AS AGED_GRP
- 似乎数字在15到29之间,它将返回&#34; 151+天&#34;,这和&#34; 0-30&#34;似乎是唯一的回归结果。
如果我能够在第一次WHEN中为该INT值添加5,为什么我无法正确评估它?
我只是希望它成为我的CASE stmt评估的数字,这样我才能得到正确的结果。在此先感谢您的帮助。
答案 0 :(得分:1)
您可以简化代码:
CASE
WHEN Days_AGED IS NULL THEN NULL -- What should happen when Days_AGED contains NULLs ?
WHEN Days_AGED <= 30 THEN '0-30 Days'
WHEN Days_AGED <= 60 THEN '31-60 Days'
WHEN Days_AGED <= 90 THEN '61-90 Days'
WHEN Days_AGED <= 120 THEN '91-120 Days'
WHEN Days_AGED <= 150 THEN '121-150 Days'
ELSE '151+ Days'
END AS AGED_GRP
或
CASE
WHEN Days_AGED IS NULL THEN NULL
WHEN Days_AGED <= 150 THEN
CONVERT(VARCHAR(11), ISNULL(NULLIF(((Days_AGED - 1) / 30) * 30, 0) + 1, 0)) + '-' +
CONVERT(VARCHAR(11), ((Days_AGED - 1) / 30 + 1) * 30) + ' Days'
ELSE '151+ Days'
END