sql 嵌套 case 语句

发布于 2024-09-08 08:33:27 字数 865 浏览 9 评论 0原文

有谁知道这个嵌套的 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

它就可以正常工作!

非常感谢任何帮助,

抱歉

,我迟到了,我错过了嵌套案例中的时间

does anyone know whats wrong with this nested select statement? It complains about missing )'s but i can't understand why it doesn't work (i have left off the other bits of the statement)

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

If i just do

    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

It works fine!

any help is much appreciated

M

sorry being a tard i'm missing the whens from the nested cases

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2024-09-15 08:33:27

它应该是:

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

即每种情况只是“WHEN”而不是“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

It should be:

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

i.e. just "WHEN" not "ELSE CASE WHEN" for each case.

I'd be tempted to simplify to:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文