0

我一直在寻找如何做到这一点,但是由于搜索时涉及的单词,很难找到与我想要找出的内容相近的东西!

如何在创建视图中使用 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;
4

1 回答 1

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
于 2013-06-05T00:42:29.380 回答