简体   繁体   English

在Where子句SQL Server中执行IF语句

[英]Executing IF statement in Where clause SQL Server

I'm looking to only return rows where if my first condition doesn't exist then return new condition from same table, and additional conditions: basically...: 我只想返回不存在我的第一个条件的行,然后从同一张表返回新条件,以及其他条件:基本上...:

SELECT *   
FROM Table1     
WHERE

Condition#1: Column1=Column2

Condition#2: AND IF Column1 = Column2 doesn't exist (matching values for same ID) 
             THEN return results where Column3 = Column4

在此处输入图片说明

Thanks. 谢谢。

I suspect that this will work for what the OP is after, however, the logic seems odd... 我怀疑这将适用于OP之后的工作,但是逻辑似乎很奇怪...

SELECT {Columns}
FROM YourTable YT
WHERE YT.Column1 = YT.Column2
   OR (YT.Column3 = YT.Column4
  AND  NOT EXISTS(SELECT 1
                  FROM YourTable e
                  WHERE YT.id = e.id
                    AND e.Column1 = e.Column2));

Not sure how good the performance will be here though, as you may well end up with a table scan with that NOT EXISTS . 但是,不确定性能会如何,因为您可能最终使用NOT EXISTS进行表扫描。

Well, based off the comments, here is one way. 好吧,根据评论,这是一种方法。

SELECT *    
FROM Table1      
WHERE Column1 = Column2 
UNION 
SELECT * 
FROM Table2 
WHERE  Column1 != Column2 and Column3 = Column4

You can do this by using simple booleans: 您可以使用简单的布尔值来做到这一点:

WHERE
Column1 = Column2
OR
Column3 = Column4

The Venn diagram for this: 维恩图为此:

在此处输入图片说明

All of the shaded area will be returned, including when Column1 = Column2 & Column3 = Column4 (the dark shaded intersection) 将返回所有阴影区域,包括当Column1 = Column2和Column3 = Column4时(深色阴影交叉点)

If you don't want rows where both conditions are met then you can modify the logic: 如果您不希望同时满足这两个条件的行,则可以修改逻辑:

WHERE
Column1 = Column2
OR
(Column1 <> Column2 
AND
Column3 = Column4)

The Venn would look the same except the intersection would be white and excluded. 除了交叉点为白色且不包括交点以外,维恩看起来将相同。

First you need to check what case you are. 首先,您需要检查您的情况。 So you count how many C1 = C2 you have in your table 因此,您计算表中有多少C1 = C2

SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2
FROM Table1
GROUP BY [id]

Then you have two conditions 那你有两个条件

first when you have some c1 = c2 you do 首先,当你有一些c1 = c2时,你会做

  (c1 = c2 and c1_c2 > 0 )

and the second when dont have any c1 = c2 当没有c1 = c2时第二个

  (c1_c2 = 0 and c3 = c4)

SQL DEMO: : Because is an OR and the c1_c2 count decide which side of the where is going to work. SQL DEMO ::因为是OR ,c1_c2的计数决定将在何处工作。

WITH c12 as (
    SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2_cnt
    FROM Table1
    GROUP BY [id]
) 
SELECT *
FROM Table1 T
JOIN c12 C
  ON T.[id] = C.[id]  
WHERE  ( C.c1_c2_cnt > 0 and T.c1 = T.c2 )
   OR  ( C.c1_c2_cnt = 0 and T.c3 = T.c4 )

OUTPUT OUTPUT

在此处输入图片说明

Another approach : 另一种方法:

DECLARE 
    @t TABLE( ID INT, col1 CHAR(1), col2 CHAR(1), col3 INT, col4 INT)

INSERT INTO @t VALUES 
(1234,'A','B',100,100),
(1234,'A','A',100,100),
(2468,'A','C',125,125),
(2468,'A','B',100,150)

SELECT 
    ID
,   col1
,   col2
,   col3
,   col4
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, col1 ORDER BY col2) RN
    FROM (
        SELECT *
        FROM @t
        WHERE 
            col3 = col4
        AND col1 = col2
        UNION ALL 
        SELECT *
        FROM @t
        WHERE 
            col3 = col4
        AND ID NOT IN(
            SELECT ID
            FROM @t
            WHERE 
                col3 = col4
            AND col1 = col2
            )
    ) D
) E
WHERE 
    RN = 1

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

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