简体   繁体   English

WHEN内的SQL Case

[英]SQL Case inside WHEN

May be this is silly but can we write a case inside another case's WHEN ? 也许这很愚蠢,但是我们可以在另一个案例的WHEN里面写一个案例吗?

Below code working for me but I am not sure if this is correct. 下面的代码为我工作,但我不确定这是否正确。

SELECT
     (SUM(CASE
            WHEN (
                  CASE
                      WHEN r.status < b.status
                      THEN r.status
                      ELSE b.status
                  END
                 ) = '4' 
            THEN 1
            ELSE 0
         END)
     ) AS WORKED
FROM
    tbl1 r, tbl2 b

All the examples on nested cases are like CASE inside a THEN so I am not sure if this a good practice. 关于嵌套案例的所有示例都类似于THEN内的CASE,因此我不确定这是否是一个好习惯。 Is there a better way to get the same results? 有没有更好的方法来获得相同的结果?

Yes you can. 是的你可以。 MSDN also informs us that in SQL SERVER, you can only have a maximum of 10 CASE expressions embedded into each other. MSDN还通知我们,在SQL SERVER中,彼此之间最多只能嵌入10个CASE表达式。 Oddly enough, a search for ORACLE up negative about this potential limitation. 奇怪的是,对ORACLE的搜索对这一潜在限制产生了负面影响。 Probably important to note. 可能重要的是要注意。

Of course, you can also just use more WHEN (up to 255 in ORACLE) statements, too, but that only works if you do not need to nest your logic (such as comparing two different columns values) 当然,您也可以使用更多的WHEN语句(在ORACLE中最多为255),但这仅在不需要嵌套逻辑(例如比较两个不同的列值)的情况下才有效。

Sources: https://msdn.microsoft.com/en-us/library/ms181765.aspx http://www.techonthenet.com/oracle/functions/case.php 来源: https//msdn.microsoft.com/zh-cn/library/ms181765.aspx http://www.techonthenet.com/oracle/functions/case.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM