我可以为 SQL Server 中的另一个选择选择声明的键吗

问题描述 投票:0回答:1
(CASE WHEN Status IN ('Selection Process','Approved','Closed') THEN
    CASE WHEN TotalLayerApproval=1 THEN ApprDate1 ELSE
        CASE WHEN TotalLayerApproval=2 THEN ApprDate2 ELSE 
            CASE WHEN TotalLayerApproval=3 THEN ApprDate3 ELSE 
                CASE WHEN TotalLayerApproval=4 THEN ApprDate4 ELSE 
                    CASE WHEN TotalLayerApproval=5 THEN ApprDate5 ELSE 
                        CASE WHEN TotalLayerApproval=6 THEN ApprDate6 ELSE 
                            CASE WHEN TotalLayerApproval=7 THEN ApprDate7 ELSE 
                                CASE WHEN TotalLayerApproval=8 THEN ApprDate8 ELSE 
                                    CASE WHEN TotalLayerApproval=9 THEN ApprDate9 ELSE NULL END
                                END 
                            END 
                        END 
                    END 
                END 
            END 
        END 
    END
ELSE NULL END) AS Date_Approved,
(CASE WHEN Date_Approved IS NULL THEN NULL 
ELSE DATEDIFF(day,Date_Approved, CONVERT(date,GETDATE())) END) AS SLA

例如上面的(

Date_Approved
),或者有人可以再次简化它,这样就不会出现太多情况

sql sql-server ssms
1个回答
0
投票

无需进行疯狂的嵌套......只需使用:

CASE 
    WHEN Status IN ('Selection Process', 'Approved', 'Closed') 
        THEN
            CASE 
                WHEN TotalLayerApproval = 1 THEN ApprDate1 
                WHEN TotalLayerApproval = 2 THEN ApprDate2 
                WHEN TotalLayerApproval = 3 THEN ApprDate3 
                WHEN TotalLayerApproval = 4 THEN ApprDate4 
                WHEN TotalLayerApproval = 5 THEN ApprDate5 
                WHEN TotalLayerApproval = 6 THEN ApprDate6 
                WHEN TotalLayerApproval = 7 THEN ApprDate7 
                WHEN TotalLayerApproval = 8 THEN ApprDate8 
                WHEN TotalLayerApproval = 9 THEN ApprDate9 
                ELSE NULL 
            END 
        ELSE NULL 
END AS Date_Approved,
© www.soinside.com 2019 - 2024. All rights reserved.