【发布时间】: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