如何排除列值不同但其他列相同的所有行
How to exclude all rows where a column value is different but other columns are the same
这么多类似的问题 - 主要问题是关于如何 select 只有一个列不同的重复项之一,但我想从查询中排除所有这些,并且只得到特定字段没有不同的字段。
我正在寻找状态为 -1 的所有 reference_no
,但对于同一个 reference_no,状态同时为 -1 和 1 的那些除外,如 [=22] =] 下面。查询应该 return 只有行 ID 4。我该怎么做?
这是使用 SQL 服务器 2016
| id | process_date | status | reference_no |
| --- | ------------ | ------ | ----------- |
| 1 | 12/5/22 | 1 | 789456 |
| 2 | 12/5/22 | -1 | 789456 |
| 3 | 12/5/22 | 1 | 789456 |
| 4 | 12/5/22 | 1 | 321654 |
如果我没理解错你想要一个不存在检查
select *
from t
where status = -1
and not exists (
select * from t t2
where t2.status = 1 and t2.reference_no = t.reference_no
);
这么多类似的问题 - 主要问题是关于如何 select 只有一个列不同的重复项之一,但我想从查询中排除所有这些,并且只得到特定字段没有不同的字段。
我正在寻找状态为 -1 的所有 reference_no
,但对于同一个 reference_no,状态同时为 -1 和 1 的那些除外,如 [=22] =] 下面。查询应该 return 只有行 ID 4。我该怎么做?
这是使用 SQL 服务器 2016
| id | process_date | status | reference_no |
| --- | ------------ | ------ | ----------- |
| 1 | 12/5/22 | 1 | 789456 |
| 2 | 12/5/22 | -1 | 789456 |
| 3 | 12/5/22 | 1 | 789456 |
| 4 | 12/5/22 | 1 | 321654 |
如果我没理解错你想要一个不存在检查
select *
from t
where status = -1
and not exists (
select * from t t2
where t2.status = 1 and t2.reference_no = t.reference_no
);