【问题标题】:Excluding rows from query results based on column value根据列值从查询结果中排除行
【发布时间】:2016-10-18 21:13:55
【问题描述】:

我正在尝试删除列 B = 'S' 的所有列 A 值的行。这是一个我可以用数据展示的例子:

    column A    column B
    100         S
    100         P
    100         C
    101         P
    101         C
    102         S
    103         C
    104         P

从这里,我想从 A 列中删除所有在 B 列(100 和 102)中显示“S”的条目,所以我只剩下:

column A    column B
    101         P
    101         C
    103         C
    104         P 

我试图遵循类似 SO 帖子 (Exclude rows with a column containing a value if multiple rows exist for) 中的步骤,但它一直排除存在“S”的行并保留共享列 A 值。

例如,这是我正在处理的查询的相关部分:

select table_a.column_a
    ,table_b.column_b
    ,...
from table_z
inner join table_b
    on table_z.z = table_b.z
inner join table_y
    on table_z.y = table_y.y
left outer join table_a
    on table_x.x = table_a.x
where date > 'YYYY-MM-DD'
    and (
        table_b.column_b not in (
            select column_b
            from table_b
            where (column_b = 'S')
            )
        )
order by table_a.column_a

但它只会删除 column_b = 'S' 的行,并且不会删除 column_A 值与 column_b 出现位置匹配的行(本文开头的 column_a = 100 示例)。

【问题讨论】:

    标签: sql sql-server database sql-server-2008-r2


    【解决方案1】:
    Declare @YourTable table (ColumnA int,ColumnB varchar(25))
    Insert Into @YourTable values
    (100,'S'),
    (100,'P'),
    (100,'C'),
    (101,'P'),
    (101,'C'),
    (102,'S'),
    (103,'C'),
    (104,'P')
    
    Select * 
     From  @YourTable
     Where ColumnA Not In (Select Distinct ColumnA From @YourTable where ColumnB='S')
    

    返回

    ColumnA ColumnB
    101     P
    101     C
    103     C
    104     P
    

    【讨论】:

      【解决方案2】:
      SELECT *
        FROM YourTable
       WHERE column_A IN ( SELECT column_A FROM YourTable
                           EXCEPT
                           SELECT column_A FROM YourTable 
                            WHERE column_B = 'S' );
      

      【讨论】:

        【解决方案3】:

        我认为您需要在 where 子句的子查询中使用 Table_A,而不是 Table_B:

            select *
        from Table_A
        where Column_B != 'S'
            and Column_A not in (
                select distinct column_A
                from Table_A
                where Column_B = 'S'
                )
        

        编辑了您的查询。这应该有效:

        select table_a.column_a
            ,table_b.column_b
            ,...
        from table_z
        inner join table_b
            on table_z.z = table_b.z
        inner join table_y
            on table_z.y = table_y.y
        left outer join table_a
            on table_x.x = table_a.x
        where date > 'YYYY-MM-DD'
            and table_a.column_b != 'S'
            and table_a.column_A not in (
                select distinct table_a.column_A
                from Table_A
                where Column_B = 'S'
                )
        order by table_a.column_a
        

        【讨论】:

          【解决方案4】:
          SELECT *
          FROM   Table_A as A
          WHERE  NOT EXISTS (
            SELECT *
            FROM   Table_B as B
            WHERE  B.ColumnA = A.ColumnA
            AND    B.ColumnB = 'S' )
          

          这与另一个答案非常相似,只是它使用 NOT EXISTS 而不是 NOT IN。 IN 关键字可能非常有用,但using NOT IN should be avoided 可能出于性能原因。

          【讨论】:

            猜你喜欢
            • 2015-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-12
            • 2017-05-09
            • 2020-04-19
            相关资源
            最近更新 更多