我一直在寻找如何做到这一点,但是由于搜索时所涉及的词语非常难以找到与我想要找到的内容相近的东西!
如何在创建视图中使用sql CASE表达式?
有人可以告诉我正确的语法吗?
以下是我的目前情况,但它无法正常工作。
create view vw_price as
select vehicle.price
(case when price between 0 and 999 then ‘0-999’
when price between 1000 and 1999 then ‘1000-1999’
when price between 2000 and 2999 then ‘2000-2999’
when price between 3000 and 3999 then ‘3000-3999’
when price between 4000 and 4999 then ‘4000-4999’
when price between 5000 and 5999 then ‘5000-5999’
when price between 6000 and 6999 then ‘6000-6999’
when price between 7000 and 7999 then ‘7000-7999’
when price between 8000 and 8999 then ‘8000-8999’
when price between 9000 and 9999 then ‘9000-9999’
end) as price_group from vehicle;
答案 0 :(得分:1)
以下语法应该可以创建所需的视图。我认为你在案例陈述之前只是错过了一个逗号。
USE [<<<database_name>>>]
GO
CREATE VIEW vw_price
AS
select price,
(case
when price between 0 and 999 then '0-999'
when price between 1000 and 1999 then '1000-1999'
when price between 2000 and 2999 then '2000-2999'
when price between 3000 and 3999 then '3000-3999'
when price between 4000 and 4999 then '4000-4999'
when price between 5000 and 5999 then '5000-5999'
when price between 6000 and 6999 then '6000-6999'
when price between 7000 and 7999 then '7000-7999'
when price between 8000 and 8999 then '8000-8999'
when price between 9000 and 9999 then '9000-9999'
end) "price_group" from vehicle;
GO