我正在编写一个 SQL 查询,其中返回的一些列需要根据相当多的条件进行计算。
我目前正在使用嵌套的 case 语句,但它变得混乱。有更好的(更有组织性和/或可读性)方法吗?
(我使用的是 Microsoft SQL Server,2005)
一个简化的例子:
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
您可以尝试某种 COALESCE 技巧,例如:
选择合并( CASE WHEN 条件1 THEN 计算1 ELSE NULL END, CASE WHEN 条件2 THEN 计算2 ELSE NULL END, ETC... )
将所有这些案例合并为一个。
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
您可以组合多个条件来避免这种情况:
CASE WHEN condition1 = true AND condition2 = true THEN calculation1
WHEN condition1 = true AND condition2 = false THEN calculation2
ELSE 'what so ever' END,
我个人就是这样做的,限制嵌入的 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
这是嵌套“复杂”案例语句的简单解决方案: --嵌套大小写复杂表达式
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
只需确保每个案例陈述都有结束陈述
我们可以将多个条件组合在一起以减少性能开销。
假设我们要对三个变量 a b c 执行案例。我们可以这样做:
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,
用户定义的函数可能会更好,至少隐藏逻辑 - 尤其是。如果您需要在多个查询中执行此操作
我经历了这个,发现所有答案都超级酷,但是想添加到@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
您可以将 ELSE 设置为可选,因为它不是强制性的,这在很多情况下都非常有帮助。
格式不太复杂。确保保留缩进,以免在 END 中丢失。
SELECT
CASE WHEN 1=1 THEN
CASE
WHEN 11=11 THEN CASE WHEN 111=111 THEN '1-11-111' END
WHEN 12=12 THEN CASE WHEN 122=122 THEN '1-12-122' END
WHEN 13=13 THEN CASE WHEN 133=133 THEN '1-13-133' END
END
ELSE
CASE WHEN 2=2 THEN
CASE
WHEN 21=21 THEN CASE WHEN 211=211 THEN '2-21-211' END
WHEN 22=22 THEN CASE WHEN 222=222 THEN '2-22-222' END
END
ELSE
CASE WHEN 3=3 THEN
CASE
WHEN 31=31 THEN CASE WHEN 311=311 THEN '3-31-311' END
WHEN 32=32 THEN CASE WHEN 322=322 THEN '3-32-322' END
END
ELSE
CASE WHEN 4=4 THEN
CASE
WHEN 41=41 THEN CASE WHEN 411=411 THEN '4-41-411' END
WHEN 42=42 THEN CASE WHEN 412=412 THEN '4-42-412' END
END
ELSE
CASE
WHEN 51=51 THEN CASE WHEN 511=511 THEN '51-511' END
WHEN 52=52 THEN CASE WHEN 512=512 THEN '52-512' END
WHEN 53=53 THEN CASE WHEN 513=513 THEN '53-513' END
WHEN 54=54 THEN CASE WHEN 514=514 THEN '54-514' END
END
END
END
END
END
AS Sku
FROM YourTable