[英]Subquery in CASE statement under the WHERE clause (using IN)
I have the query below giving me this error: 我有下面的查询给我这个错误:
Msg 512, Level 16, State 1, Line 5 消息512,级别16,状态1,第5行
Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
I'm getting the error from the subquery in the WHERE
clause. 我从WHERE
子句中的子查询中获取错误。
When you see '5,137' that will be a variable taking its place. 当您看到'5,137'时,它将取代它。 And Select val FROM DB01.dbo.f_split('5,137',',')
will return the list of separated values as a result. 然后, Select val FROM DB01.dbo.f_split('5,137',',')
将返回结果列表。
Another thing I tried was placing ('5','137')
in place of (Select val FROM DB01.dbo.f_split('5,137',','))
but I was getting an error on the comma between 5 and 137. 我尝试的另一件事是将('5','137')
替换为(Select val FROM DB01.dbo.f_split('5,137',','))
但逗号5和137之间却出现错误。
Any ideas? 有任何想法吗? All help is greatly appreciated. 非常感谢所有帮助。
select
@Total_Orders = sum(a11.ORDER_CNT)
from
a11
join
a12 on (a11.STORE_ID = a12.STORE_ID)
join
a13 on (a12.CLIENT_ID = a13.CLIENT_ID)
join
a14 on (a11.ACTIV_DATE_ID = a14.DATE_ID)
join
a15 on (a13.PARENT_ID = a15.PARENT_ID)
where
a15.PARENT_DESC = 'Vanilla'
AND a13.CLIENT_ID IN
(
CASE WHEN '5,137'<>'All'
THEN (Select val FROM DB01.dbo.f_split('5,137',','))
ELSE (a13.CLIENT_ID)
END
)
AND a14.DATE between CONVERT(char(10), '2015-12-27T00:00:00-05:00',126) and CONVERT(char(10), '2016-01-02T23:59:59-05:00',126)
group by a13.PARENT_ID
CASE
expressions can only return single scalar values. CASE
表达式只能返回单个标量值。 Separate the two legs and us OR
: 分开两条腿,然后我们OR
:
AND ('5,137' = 'All'
OR a13.CLIENT_ID IN (Select val FROM DB01.dbo.f_split('5,137',',')))
CASE
does not work like if
in procedural languages. CASE
不能像if
过程语言那样工作。 CASE
only returns a single value. CASE
仅返回单个值。 So you'll have to rewrite your WHERE
clause along the lines of: 因此,您必须按照以下方式重写WHERE
子句:
WHERE ('5,137'<>'All'
AND a13.CLIENT_ID IN (Select val FROM DB01.dbo.f_split('5,137',',')))
OR '5,137'='All'
I hope I understand correctly that '5,137'
is a placeholder, which will be filled in with different values. 我希望我正确理解'5,137'
是一个占位符,它将用不同的值填充。 Otherwise it's pointless to compare it to 'All'
否则,将其与'All'
进行比较毫无意义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.