简体   繁体   English

如何在SQL查询中两次使用CASE子句

[英]How can I use CASE clause twice in SQL Query

I am trying to apply two conditions in one SQL Query. 我试图在一个SQL查询中应用两个条件。

(select DISTINCT (
        CASE WHEN (
         ABC.GemUserID = '99' ) 
         OR ABC.GemUserID != '99'
            THEN 'Yes'
    ELSE 'No'
        END)) AS AllWell

This gives me output as "Yes" where as the CASE is true only for 1 file like below : 这使我的输出为“是”,其中CASE仅适用于1个文件,如下所示:

Current Result: 当前结果:

99 , Yes
99 , Yes
99 , Yes

Expected Result: 预期结果:

99 , No
99 , No
99 , Yes

I am using the below query but the SQL Query Intellisence is identifying it as wrong. 我正在使用以下查询,但SQL查询智能将其标识为错误。

Wrong Query: 错误的查询:

(select DISTINCT (
    CASE WHEN ( ABC.GEMUserID  = '99' THEN 'Yes' else 'No'
    CASE WHEN ( ABC.GEMUserID != '99' THEN 'No'  else 'Yes'

    END)) AS AllWell

After fixing the above Wrong Query: 解决上述错误查询后:

(select DISTINCT 
        (CASE WHEN  ABC.GemUserID  = '99' THEN 'Yes' else 'No' END), 
        (CASE WHEN  ABC.GemUserID != '99' THEN 'No'  else 'Yes' END))
         AS AllWell

But I am getting error: 但我得到了错误:

Msg 116, Level 16, State 1, Line 17 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. 消息116,级别16,状态1,行17当未将EXISTS引入子查询时,只能在选择列表中指定一个表达式。

How to fix this? 如何解决这个问题?

select distinct is -- itself -- part of SQL syntax. select distinct是-本身-SQL语法的一部分。 The distinct is not a function. distinct不是功能。 It should not be followed by parentheses. 不应在其后加上括号。 So, if I understand your question: 因此,如果我理解您的问题:

select DISTINCT 
       ( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
       ( CASE WHEN ABC.GEMUserID <> '99' THEN 'No'  else 'Yes' END) as AllWell

Do you plan on giving the first column a name? 您打算给第一栏命名吗?

select DISTINCT 
CASE WHEN  ABC.GEMUserID  = '99' THEN 'Yes' 
     ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell

According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. 根据该错误,您的查询是较大SQL命令中的子查询(可能在IN?之后)。 Therefore, it is not possible for such subquery to return more than one column . 因此,此类子查询不可能返回多个column

So your first query, you've said: 因此,您的第一个查询是:

CASE WHEN userID = 99 OR userID != 99

In other words: 换一种说法:

CASE WHEN 1=1

This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows). 这就是为什么它对所有内容返回yes的原因(不确定当前和预期结果之间的差异应考虑到所有行的userID为99)。

For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). 对于您的错误查询,似乎您是在另一个选择的中间返回该选择(因为您在最后加上了别名)。 Due to that, you cannot return more than one column in your nested select. 因此,嵌套选择中不能返回多个列。 You do not need the second CASE statement, simply change your query to: 您不需要第二条CASE语句,只需将查询更改为:

(select DISTINCT
    CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell

Assuming that you hold the missing pieces to the query such as the FROM. 假设您将缺少的部分保留在查询中,例如FROM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM