带有 datedfif 和 between 的案例陈述
Case statement with datedfif and between
我正在使用 Sybase IQ,并且有以下 SQL 代码似乎不起作用。问题出在 case 语句上。提前致谢
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'
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
:
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'
我正在使用 Sybase IQ,并且有以下 SQL 代码似乎不起作用。问题出在 case 语句上。提前致谢
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'
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
:
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'