【发布时间】:2021-06-02 18:44:08
【问题描述】:
我有以下带有 13 个参数的非嵌套 case 语句(可以看出,只有一个 case 语句)的代码,但我收到一条错误消息
“Case 表达式只能嵌套到第 10 级”。
当我使用较少的参数时它确实有效,但这并没有真正帮助我,因为我需要我所需结果集的所有参数。
select ID,
CASE
when sum(Y) between 0 and 30 then 1
when sum(Y) between 31 and 70 then 2
when sum(Y) between 71 and 100 then 3
when sum(Y) between 101 and 200 then 4
when sum(Y) between 201 and 300 then 5
when sum(Y) between 301 and 400 then 6
when sum(Y) between 401 and 500 then 7
when sum(Y) between 501 and 600 then 8
when sum(Y) between 601 and 700 then 9
when sum(Y) between 701 and 800 then 10
when sum(Y) between 801 and 900 then 11
when sum(Y) between 901 and 1000 then 12
when sum(Y) > 1000 then 13
end
from X
group by ID
;
我确实设法解决了这个问题,将我的 case 参数拆分为两个不同的 select 语句,并在它们之间使用 UNION 来获得我所需的结果集,但感觉好像我可以做得更好,因为原始案例论证不是 n 估计
【问题讨论】:
-
你是sending this to a linked server吗?我不能像写的那样复制这个。这显示没有错误dbfiddle.uk/…
-
您可以通过将
between 101 and 1000折叠到单个案例分支并使用一些数学计算结果来解决此问题 -
此时,将当前结果集加入到包含必要范围和最终值的表(表达式)中似乎是一个更好的主意,而不是在 @ 中枚举每个范围987654325@ 子句。
-
@GordonLinoff 我知道。这已经在我原来的评论中超链接了
-
@MartinSmith 。 . .糟糕,我错过了。