在 SQL Server 中执行嵌套 case 语句逻辑的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/505747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:59:07  来源:igfitidea点击:

Best way to do nested case statement logic in SQL Server

sqlsql-serversql-server-2005selectnested

提问by Sophia

I'm writing an SQL Query, where a few of the columns returned need to be calculated depending on quite a lot of conditions.

我正在编写一个 SQL 查询,其中需要根据很多条件计算返回的一些列。

I'm currently using nested case statements, but its getting messy. Is there a better (more organised and/or readable) way?

我目前正在使用嵌套的 case 语句,但它变得混乱。有没有更好(更有条理和/或可读性更强)的方式?

(I am using Microsoft SQL Server, 2005)

(我使用的是 Microsoft SQL Server,2005)



A simplified example:

一个简化的例子:

SELECT
    col1,
    col2,
    col3,
    CASE
        WHEN condition 
        THEN
            CASE
                WHEN condition1 
                THEN
                    CASE 
                        WHEN condition2
                        THEN calculation1
                        ELSE calculation2
                    END
                ELSE
                    CASE 
                        WHEN condition2
                        THEN calculation3
                        ELSE calculation4
                    END
            END
        ELSE 
            CASE 
                WHEN condition1 
                THEN 
                    CASE
                        WHEN condition2 
                        THEN calculation5
                        ELSE calculation6
                    END
                ELSE
                    CASE
                        WHEN condition2 
                        THEN calculation7
                        ELSE calculation8
                    END
            END            
    END AS 'calculatedcol1',
    col4,
    col5 -- etc
FROM table

采纳答案by Chris KL

You could try some sort of COALESCE trick, eg:

您可以尝试某种 COALESCE 技巧,例如:

SELECT COALESCE(
  CASE WHEN condition1 THEN calculation1 ELSE NULL END,
  CASE WHEN condition2 THEN calculation2 ELSE NULL END,
  etc...
)

回答by Deejers

Wrap all those cases into one.

将所有这些案例合二为一。



SELECT
    col1,
    col2,
    col3,
    CASE
        WHEN condition1 THEN calculation1 
        WHEN condition2 THEN calculation2
        WHEN condition3 THEN calculation3
        WHEN condition4 THEN calculation4
        WHEN condition5 THEN calculation5
        ELSE NULL         
    END AS 'calculatedcol1',
    col4,
    col5 -- etc
FROM table

回答by ssingh

You can combine multiple conditions to avoid the situation:

您可以组合多个条件来避免这种情况:

CASE WHEN condition1 = true AND condition2 = true THEN calculation1 
     WHEN condition1 = true AND condition2 = false 
     ELSE 'what so ever' END,

回答by beach

I personally do it this way, keeping the embedded CASE expressions confined. I'd also put comments in to explain what is going on. If it is too complex, break it out into function.

我个人是这样做的,限制了嵌入的 CASE 表达式。我也会发表评论来解释发生了什么。如果它太复杂,请将其分解为函数。

SELECT
    col1,
    col2,
    col3,
    CASE WHEN condition THEN
      CASE WHEN condition1 THEN
        CASE WHEN condition2 THEN calculation1
        ELSE calculation2 END
      ELSE
        CASE WHEN condition2 THEN calculation3
        ELSE calculation4 END
      END
    ELSE CASE WHEN condition1 THEN 
      CASE WHEN condition2 THEN calculation5
      ELSE calculation6 END
    ELSE CASE WHEN condition2 THEN calculation7
         ELSE calculation8 END
    END AS 'calculatedcol1',
    col4,
    col5 -- etc
FROM table

回答by beach

Here's a simple solution to the nested "Complex" case statment: --Nested Case Complex Expression

这是嵌套“复杂”案例语句的简单解决方案:--Nested Case Complex Expression

select  datediff(dd,Invdate,'2009/01/31')+1 as DaysOld, 
    case when datediff(dd,Invdate,'2009/01/31')+1 >150 then 6 else
        case when datediff(dd,Invdate,'2009/01/31')+1 >120 then 5 else 
            case when datediff(dd,Invdate,'2009/01/31')+1 >90 then 4 else 
                case when datediff(dd,Invdate,'2009/01/31')+1 >60 then 3 else 
                    case when datediff(dd,Invdate,'2009/01/31')+1 >30 then 2 else 
                        case when datediff(dd,Invdate,'2009/01/31')+1 >30 then 1 end 
                    end
                end
            end
        end
    end as Bucket
from rm20090131atb

Just make sure you have an end statement for every case statement

只要确保每个 case 语句都有一个结束语句

回答by Steven A. Lowe

a user-defined function may server better, at least to hide the logic - esp. if you need to do this in more than one query

用户定义的函数可能会更好,至少可以隐藏逻辑 - 尤其是。如果您需要在多个查询中执行此操作

回答by Sanjeev Singh

We can combine multiple conditions together to reduce the performance overhead.

我们可以将多个条件组合在一起以减少性能开销。

Let there are three variables a b c on which we want to perform cases. We can do this as below:

让我们想要执行案例的三个变量 abc 。我们可以这样做:

CASE WHEN a = 1 AND b = 1 AND c = 1 THEN '1'
     WHEN a = 0 AND b = 0 AND c = 1 THEN '0'
ELSE '0' END,

回答by user3065757

I went through this and found all the answers super cool, however wants to add to answer given by @deejers

我经历了这个并发现所有答案都非常酷,但是想添加到@deejers给出的答案

    SELECT
    col1,
    col2,
    col3,
    CASE
        WHEN condition1 THEN calculation1 
        WHEN condition2 THEN calculation2
        WHEN condition3 THEN calculation3
        WHEN condition4 THEN calculation4
        WHEN condition5 THEN calculation5         
    END AS 'calculatedcol1',
    col4,
    col5 -- etc
FROM table

you can make ELSE optional as its not mandatory, it is very helpful in many scenarios.

您可以将 ELSE 设为可选,因为它不是强制性的,它在许多情况下都非常有用。

回答by Sandy

This example might help you, the picture shows how SQL case statement will look like when there are if and more than one inner if loops

这个例子可能对你有帮助,图片显示了当有 if 和多个内部 if 循环时 SQL case 语句的样子

enter image description here

在此处输入图片说明