2017-11-28 1 views
-2

Sybase IQ를 사용하고 있으며 작동하지 않는 다음 SQL 코드가 있습니다. 문제는 사례 명세서와 함께입니다. 미리 감사드립니다.datedfif가 포함 된 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' 
+1

* 작동하지 않는 것 같습니다. * 작동하지 않습니다. – Liam

+1

"작동하지 않는 것 같습니다"는 문제에 대한 설명이 아닙니다. 오류가 있습니까? 오류가 무엇입니까? 예기치 않은 결과가 있습니까? 당신은 무엇을 기대하며, 실제 결과는 무엇입니까? – HoneyBadger

답변

2

언제든지 when 절을 키워드로 사용할 필요가 없습니다. 이것을 시도하십시오 :

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' 
+0

도움을 주셔서 감사합니다 –

+0

즐거움은 내 것입니다. :) –

1

구문은 약간 꺼져 있습니다. 각 조건에 대해 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' 
+0

도움 주셔서 감사합니다. –