我正在编写报告,由于数据集的评分不同,我需要根据参数灵活处理。
我只想要null或评分结果。所以正常的where子句是(x is null or x in (1,2,3))
当我尝试在CASE WHEN
中实现它时,我会收到语法错误,
下面是我尝试的,这只是尝试数据集部分所以我不会在此部分之后硬编码参数值
DECLARE @Type NVARCHAR(30)
set @Type = foo
select COUNT(A) as A,B,C
FROM Activity act
inner join x
inner join v
inner join y
inner join z
where 1=1
and DetailKey = @Type
and cd.Value is null or IN
CASE @Type
WHEN 'foo' THEN ('1','2','3') ELSE
WHEN 'bar' THEN ('YR','OR','DR') END
编辑:为了简洁,在示例中省略了连接,它们不是问题。
答案 0 :(得分:2)
不可能将值列表作为CASE
表达式的谓词。但您可以按如下方式重新定义WHERE
条款:
WHERE
1 = 1 AND -- not sure if this is needed
DetailKey = @Type AND
(cd.Value IS NULL OR
(@Type = 'foo' AND cd.Value IN ('1','2','3')) OR
(@Type = 'bar' AND cd.Value IN ('YR', 'OR', 'DR')))