我正在使用MSSQL来运行查询,但是我想简单地通过使用循环来执行当前步骤。MSSQL_如何通过循环显示数字的范围?
-- My current script
select product, price,price_range
from
(select
product,
price,
case
when price <= 100 then 'upto100'
when price between 101 and 200 then '101-200'
when price between 201 and 300 then '201-300'
when price between 301 and 400 then '301-400'
when price between 401 and 500 then '401-500'
when price between 501 and 600 then '501-600'
when price between 601 and 700 then '601-700'
when price between 701 and 800 then '701-800'
when price >= 801 then '800+EURO'
end as price_range
from DATA) as A
现在我的脚本,但它返回我正确的结果,因为我想:
product price price_range
shoes 50 upto100
clothes 456 401-500
computer 1500 800+EURO
,但我可以把它简单?我能以某种方式使用循环而不是'case ... when ... then'吗?然后,如果价格段增加,我不必写很多'案件..'。
我试图用“声明”和“而”,但没有得到它的工作。在这种情况下如何为循环设置变量?
最好不要使用WHLE循环 – sqluser 2015-03-03 00:38:35
有什么不对您当前的一个?添加一个循环会降低速度并使其变得复杂。 – 2015-03-03 00:41:44
目前一个工作正常,因为现在我只列出少于10个组。但是如果在实际分析中我需要50个组,那么做一个循环比重复输入那些“when..then ..”要容易得多50次 – Kevin 2015-03-03 21:27:09