sql嵌套的case语句

时间:2010-07-06 15:36:39

标签: sql oracle case

有没有人知道这个嵌套的select语句有什么问题?它抱怨失踪)但是我无法理解为什么它不起作用(我已经不在声明的其他部分)

Select
(CASE WHEN REQUESTS.grade_id = 1 THEN
      (CASE WHEN  ((date_completed-date_submitted)*24*60)<=30 THEN 'Yes'
           ELSE 'No'
      END)
 ELSE CASE WHEN REQUESTS.grade_id = 2 THEN
      (CASE ((date_completed-date_submitted)*24*60) <=120 THEN 'Yes'
           ELSE 'No'
      END) 
 ELSE CASE WHEN REQUESTS.grade_id = 3 THEN
     (CASE ((date_completed-date_submitted)*24*60)<=14400 THEN 'Yes'
          ELSE 'No'
     END)
 END)in_SLA

如果我这样做

    Select
       (CASE WHEN REQUESTS.grade_id = 1 THEN
           (CASE WHEN  ((date_completed-date_submitted)*24*60)<=30 THEN 'Yes'
               ELSE 'No'
            END)
       END) in_sla

一切正常!

非常感谢任何帮助

中号

抱歉,我很想丢掉嵌套案件中的歹徒

1 个答案:

答案 0 :(得分:21)

应该是:

Select
(CASE WHEN REQUESTS.grade_id = 1 THEN
      (CASE WHEN  ((date_completed-date_submitted)*24*60)<=30 THEN 'Yes'
           ELSE 'No'
      END)
      WHEN REQUESTS.grade_id = 2 THEN
      (CASE ((date_completed-date_submitted)*24*60) <=120 THEN 'Yes'
           ELSE 'No'
      END) 
      WHEN REQUESTS.grade_id = 3 THEN
     (CASE ((date_completed-date_submitted)*24*60)<=14400 THEN 'Yes'
          ELSE 'No'
     END)
 END)in_SLA

即。对于每种情况,只是“何时”不是“ELSE CASE WHEN”。

我很想简化为:

Select
CASE WHEN (REQUESTS.grade_id = 1 AND (date_completed-date_submitted)*24*60 <= 30)
       OR (REQUESTS.grade_id = 2 AND (date_completed-date_submitted)*24*60 <=120)
       OR (REQUESTS.grade_id = 3 AND (date_completed-date_submitted)*24*60 <=14400)
     THEN 'Yes'
     ELSE 'No'
 END in_SLA