我有CASE
语句,如下所示:
SELECT CASE
WHEN <expression>> BETWEEN 0 AND 10 THEN '0-10'
WHEN <<expression>> BETWEEN 11 AND 20 THEN '11-20'
WHEN <<expression>> BETWEEN 21 AND 30 THEN '21-30'
...
这很好用,但看起来很难看。
我想将其转换为如下所示的内容:
SELECT CASE <expression>>
WHEN BETWEEN 0 AND 10 THEN '0-10'
WHEN BETWEEN 11 AND 20 THEN '11-20'
WHEN BETWEEN 21 AND 30 THEN '21-30'
...
显然第二个查询看起来更好,因为我不必多次编写那个复杂的表达式。但这是错误的。
Incorrect syntax near the keyword 'between'.
有人可以让我知道我在这里做错了吗?
更新
正如所指出的,我已经在我的问题中更改了查询以使范围独占。
答案 0 :(得分:1)
您想要的是不可能的,因为between
语句的语法需要一个表达式来测试它作为它的第一个参数:
test_expression [ NOT ] BETWEEN begin_expression AND end_expression
所以你仍然坚持使用第一种语法。
在旁注中,您的范围在使用包含范围之间有点偏差,但是案例表达式按顺序进行评估,因此范围实际上是:
WHEN <<expression>> BETWEEN 0 AND 10 THEN '0-10'
WHEN <<expression>> BETWEEN 11 AND 20 THEN '11-20'
WHEN <<expression>> BETWEEN 21 AND 30 THEN '21-30'
答案 1 :(得分:1)
使用子查询,但也可以更清晰地优化案例:
SELECT ...,
CASE
WHEN calc >= 100 THEN 'Over 100'
ELSE CONCAT(calc * 10, ' - ', calc * 10 + 9)
END AS range
FROM (SELECT *, FLOOR(<<expression>> / 10) as calc
FROM tableName) x
FLOOR()
函数向下舍入到最接近所提供数字的整数。首先将表达式除以10,然后将地板,然后乘以10,得到表达式范围的开始。例如,如果表达式结果为63.4,63.4 --> 6.34 --> 6 --> 60
如果没有要求顶级&#34;超过某些值&#34;,您可以完全省略CASE,只需CONCAT(calc * 10, ' - ', calc * 10 + 9)
即可处理任何范围内的值。
请注意,您的逻辑中存在错误:范围应为0-9,10-19 ......等。此查询也解决了这个问题。
答案 2 :(得分:0)
您可以尝试使用子查询
SELECT CASE
WHEN ColName BETWEEN 0 AND 10 THEN '0-10'
WHEN ColName BETWEEN 10 AND 20 THEN '10-20'
WHEN ColName BETWEEN 20 AND 30 THEN '20-30'
...
FROM
(
SELECT <<expression>> as ColName
FROM tableName
) Z
答案 3 :(得分:0)
案例陈述有2个变体,两者都有不同,
1. This is used when condition is dyanmically change and output also want to change
SELECT CASE
WHEN <<expression>> or condition1 THEN output1
WHEN <<expression>> or condition2 THEN output2
WHEN <<expression>> or condition3 THEN output3
WHEN <<expression>> or condition4 THEN output4
END
2. This is used when condition is same , only output is different same like swith case in c#.
SELECT CASE <<expression>>
when result1 then output1
when result2 then output2
when result3 then output3
when result4 then output4
END
对于你的问题。在每个when statement
中给出条件。
SELECT CASE
WHEN <expression>> BETWEEN 0 AND 10 THEN '0-10'
WHEN <expression>> BETWEEN 10 AND 20 THEN '10-20'
WHEN <expression>> BETWEEN 20 AND 30 THEN '20-30'
SELECT CASE
WHEN 1 = 1 THEN 'YES' --NEED THE EXPRESSION OF EACH WHEN, IF RESULT IS TRUE, THEN "THEN STATMENT IS OUTPU ELSE NEXT WHEN STATEMENT
WHEN 2 = 2 THEN 'YES'
WHEN 1 = 2 THEN 'NO'
ELSE --DEFAULT VALUE IF ALLABOVE FAILED
'NA'
END
答案 4 :(得分:0)
使用CTE:
;WITH cte as
(
SELECT <expression>> AS Expression FROM TableName
)
SELECT CASE
WHEN Expression BETWEEN 0 AND 10 THEN '0-10'
WHEN Expression BETWEEN 10 AND 20 THEN '10-20'
WHEN Expression BETWEEN 20 AND 30 THEN '20-30'
END AS C
FROM cte
如果表达总是&gt; 0和间隔是连续的,那么你甚至可以缩短它:
SELECT CASE
WHEN Expression <= 10 THEN '0-10'
WHEN Expression <= 20 THEN '10-20'
WHEN Expression <= 30 THEN '20-30'
END AS C
FROM cte
答案 5 :(得分:0)
使用CROSS / OUTER APPLY表示
DECLARE @a AS TABLE (
a int ,
b INT
);
INSERT INTO @a VALUES (1,2),(4,5),(4,1),(21,32),(32,21),(43,2);
SELECT CASE
WHEN expr BETWEEN 0 AND 10 THEN '0-10'
WHEN expr BETWEEN 11 AND 20 THEN '11-20'
WHEN expr BETWEEN 21 AND 30 THEN '21-30'
END
FROM @a
CROSS APPLY (SELECT CASE WHEN a > b THEN a ELSE b END expr) b
答案 6 :(得分:0)
在您的特定情况下,可以执行以下操作。通常-是-我也缺少此选项。
用tst作为(从(values(100,95),选择
大小写(end_n-start_n)/ 10
当为0时为'bucket_0_10'
当1时为'bucket_10_20'
当2时为'bucket_10_30'
其他'bucket_other'
结束
来自tst
但这是静态的。具有上述技巧的简单查询且没有“大小写”,将使其变得动态