带有datedfif和之间的case语句

时间:2017-11-28 10:42:54

标签: sql case sybase datediff sybase-iq

我正在使用Sybase IQ,并且具有以下似乎不起作用的SQL代码。问题在于案例陈述..提前致谢

SELECT  a.cusid, start_date, effective_dt, 
case when DATEDIFF(DAY, start_date, effective_dt) >= 5476 THEN 'Green'
case when DATEDIFF(DAY, start_date, effective_dt) between 2921 AND 4575 THEN 'Red'
case when DATEDIFF(DAY, start_date, effective_dt) BETWEEN 1096  AND 2920 THEN 'Blue'
case when DATEDIFF(DAY, start_date, effective_dt) BETWEEN 0 AND 1095 THEN 'Rose'
ELSE NULL END as tier
FROM   tablea a
INNER JOIN tableb b
    ON a.cusid = b.cusid
WHERE   b.active = 'Yes' 

2 个答案:

答案 0 :(得分:2)

每次使用when子句时都不需要case关键字。试试这个:

SELECT  a.cusid, start_date, effective_dt, 
case when DATEDIFF(DAY, start_date, effective_dt) >= 5476 THEN 'Green'
when DATEDIFF(DAY, start_date, effective_dt) between 2921 AND 4575 THEN 'Red'
when DATEDIFF(DAY, start_date, effective_dt) BETWEEN 1096  AND 2920 THEN 'Blue'
when DATEDIFF(DAY, start_date, effective_dt) BETWEEN 0 AND 1095 THEN 'Rose'
ELSE NULL END as tier
FROM   tablea a
INNER JOIN tableb b
    ON a.cusid = b.cusid
WHERE   b.active = 'Yes'

答案 1 :(得分:1)

您的语法略有偏差。对于每种情况,您都不需要window_width

case