我正在使用Sybase IQ,并且具有以下似乎不起作用的SQL代码。问题在于案例陈述..在此先感谢
SELECT a.cusid,start_date,effective_dt,情况……
每次使用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'
你的语法略有偏差。你不需要 case 对于每个条件:
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'