首页 > 解决方案 > 使用一个选择语句如何获得下表结果

问题描述

在这种情况下,需要得到的输出是每个 id 的值应该只有一个值分配给该 id(col1)。如果相同的 id 具有差异值,则无需获取该值并输出。

下面是一个示例表

输入表'Demo'

col1   col2
100     A
100     A
100     A
100     A
101     A
101     B
102     A
102     B
102     B
102     C

输出

col1   col2
100    A
100    A
100    A
100    A

标签: sql-server

解决方案


您可以使用not exists

select d.*
from Demo d
where not exists (select 1 from Demo d1 where d1.col1 = d.col1 and d1.col2 <> d.col2);

推荐阅读