SQL中的嵌套Case语句

时间:2015-04-27 17:27:34

标签: sql oracle case-statement

我收到一条错误,指出错过右括号,但我不能在我的生活中找出我的陈述中的位置。

(CASE WHEN A.AUTH_STRT_DT > CD.SVC_STRT_DT THEN 
 (CASE WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) = 1 THEN '1 DAY BEFORE' 
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) BETWEEN 2 AND 3 THEN '3 DAYS BEFORE'
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) = 4 THEN '4 DAYS BEFORE'
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) BETWEEN 5 AND 21 THEN '21 DAYS BEFORE'
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) BETWEEN 22 AND 90 THEN '90 DAY BEFORE'
      ELSE 'OVER 90' END)  
WHEN A.AUTH_END_DT < CD.SVC_END_DT THEN 
  (CASE WHEN ABS(A.AUTH_END_DT < CD.SVC_END_DT) = 1 THEN '1 DAY AFTER' 
        WHEN ABS(A.AUTH_END_DT < CD.SVC_END_DT) BETWEEN 2 AND 3 THEN '3 DAYS AFTER'
        WHEN ABS(A.AUTH_END_DT < CD.SVC_END_DT) = 4 THEN '4 DAYS AFTER'
        WHEN ABS(A.AUTH_END_DT < CD.SVC_END_DT) BETWEEN 5 AND 21 THEN '21 DAYS AFTER'
        WHEN ABS(A.AUTH_END_DT < CD.SVC_END_DT) BETWEEN 22 AND 90 THEN '90 DAYS AFTER'
        ELSE 'OVER 90' END) 
ELSE '0 DAY' END) AS DAYSLAPSED      

1 个答案:

答案 0 :(得分:3)

ABS ( x < y )无效。在第二部分中,请参阅用减号替换的条件。

(CASE WHEN A.AUTH_STRT_DT > CD.SVC_STRT_DT THEN 
 (CASE WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) = 1 THEN '1 DAY BEFORE' 
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) BETWEEN 2 AND 3 THEN '3 DAYS BEFORE'
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) = 4 THEN '4 DAYS BEFORE'
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) BETWEEN 5 AND 21 THEN '21 DAYS BEFORE'
      WHEN ABS(A.AUTH_STRT_DT - CD.SVC_STRT_DT) BETWEEN 22 AND 90 THEN '90 DAY BEFORE'
      ELSE 'OVER 90' END)  
WHEN A.AUTH_END_DT < CD.SVC_END_DT THEN 
  (CASE WHEN ABS(A.AUTH_END_DT - CD.SVC_END_DT) = 1 THEN '1 DAY AFTER' 
        WHEN ABS(A.AUTH_END_DT - CD.SVC_END_DT) BETWEEN 2 AND 3 THEN '3 DAYS AFTER'
        WHEN ABS(A.AUTH_END_DT - CD.SVC_END_DT) = 4 THEN '4 DAYS AFTER'
        WHEN ABS(A.AUTH_END_DT - CD.SVC_END_DT) BETWEEN 5 AND 21 THEN '21 DAYS AFTER'
        WHEN ABS(A.AUTH_END_DT - CD.SVC_END_DT) BETWEEN 22 AND 90 THEN '90 DAYS AFTER'
        ELSE 'OVER 90' END) 
ELSE '0 DAY' END) AS DAYSLAPSED