TSQL:在单个表中查找唯一条目

发布于 2024-08-29 03:06:52 字数 379 浏览 4 评论 0原文

考虑一个表或 CTE 结构如下:

Name    Num
----    ----
Abc     12
Abc     12
XYZ     70
XYZ     80
XYZ     85
Bar     50
Bar     55
Foo     44
Foo     44
Baz     88

要求是确定存在多个不同数字的名称

所需的结果集是

Name   
----
XYZ     
Bar     

您将使用什么 TSQL 语句来导出此结果集?

更新:确实,给定名称可能有 2 个以上条目。

Consider a table or CTE structured like this:

Name    Num
----    ----
Abc     12
Abc     12
XYZ     70
XYZ     80
XYZ     85
Bar     50
Bar     55
Foo     44
Foo     44
Baz     88

The requirement is to determine the Name where multiple different Nums exist.

The desired resultset is

Name   
----
XYZ     
Bar     

What TSQL statement would you use to derive this resultset?

Update: indeed there could be 2+ entries for a given Name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-09-05 03:06:52

假设 Num 列中没有 NULL

select
   Name
from
   MySetObject
group by
   name
having
   min(num) <> max(num)
    -- also COUNT(DISTINCT Num) > 1 achieves the same

Assumes no NULLs in Num column

select
   Name
from
   MySetObject
group by
   name
having
   min(num) <> max(num)
    -- also COUNT(DISTINCT Num) > 1 achieves the same
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文