案例语句(如果所有 NULL 或至少存在一个值)



我可能做错了,但这里有很多关于我的想法的细节。

我有一个表,我们称之为表:

Test     ID     Value
test_1   01     50
test_2   01     NULL
test_3   01     40/50
test_1   02     NULL
test_2   02     NULL

总结

我只是想做一个 CASE 表达式,如果所有测试值都为 NULL,那么如果至少一个测试值有一个值,则为"否",然后是"。

期望输出:

ID   Case
01   Yes
02   No

当前查询:

SELECT
ID
,CASE WHEN t.test IN ('test_1','test_2','test_3') IS NOT NULL
THEN 'Yes'
ELSE 'No'  END
FROM table t

错误

关键字"IS"附近的语法不正确。

使用聚合....

declare @table table (test varchar(6), id int, value varchar(24))
insert into @table
values
('test_1','01','50'),
('test_2','01',NULL),
('test_3','01','40/50'),
('test_1','02',NULL),
('test_2','02',NULL)
select
t.id
,case when min(t.value) is null and max(t.value) is null then 'NO' else 'YES' end as [Case]
from
@table t
group by
t.id

实际上,您只需要检查一次。

select
t.id
,case when max(t.value) is null then 'NO' else 'YES' end as [Case]
from
@table t
group by
t.id

相关内容

最新更新