如何创建排除不符合 CASE 语句中设置的任何条件的数据的视图?

How do create a view excluding the data that does not match any of the conditions set in a CASE statement?

我正在尝试创建一个单独的视图,该视图从另一个视图获取数据并添加一列用于分类目的;但是,就像现在一样,我有很多记录在该附加列下具有 NULL 值......我想知道我是否可以设法以某种方式排除不符合中设置的任何条件的记录我用来改变视图的 CASE 语句?提前致谢

alter view2
select col1, case when col2='love' then 'non-shop'
when col2='choose' and col3='brand' then 'non-shop'
when col2='choose' and col3<>'brand' then 'shop'
when col2='buy' then 'shop'
end traffic_type
from view1

建立我的评论:

ALTER VIEW view2 as 

;WITH CTE AS
(
select col1, case when col2='love' then 'non-shop'
when col2='choose' and col3='brand' then 'non-shop'
when col2='choose' and col3<>'brand' then 'shop'
when col2='buy' then 'shop'
end as traffic_type
from view1
)
select * from CTE
where traffic_type is not null

正如@Larnu 所说,我只是将原始查询放在这里,而不是 view1 的链接

我愿意:

alter view view2 as
    select col1, v11.traffic_type
    from view1 v1 cross apply
         ( values (case when (col2 = 'love') or (col2 = 'choose' and col3 = 'brand')
                        then 'non-shop'
                        when (col2 = 'choose' and col3 <> 'brand') or (col2 = 'buy')
                        then 'shop'
                   end)
         ) v11(traffic_type)
    where v11.traffic_type is not null;