如何查找每组中的所有行都具有特定列值的行组
示例数据:
ID1 ID2 Num Type
---------------------
1 1 1 'A'
1 1 2 'A'
1 2 3 'A'
1 2 4 'A'
2 1 1 'A'
2 2 1 'B'
3 1 1 'A'
3 2 1 'A'
所需结果:
ID1 ID2
---------
1 1
1 2
3 1
3 2
请注意,我按 ID1 和 ID2 分组,而不是按 Num 分组,并且我正在专门查找 Type = 'A' 的组。我知道可以通过在同一个表上连接两个查询来实现:一个查询用于查找具有不同类型的所有组,另一个查询用于过滤 Type = 'A' 的行。但我想知道是否可以以更有效的方式完成此操作。
我正在使用 SQL Server 2008,我当前的查询是:
SELECT ID1, ID2
FROM (
SELECT ID1, ID2
FROM T
GROUP BY ID1, ID2
HAVING COUNT( DISTINCT Type ) = 1
) AS SingleType
INNER JOIN (
SELECT ID1, ID2
FROM T
WHERE Type = 'A'
GROUP BY ID1, ID2
) AS TypeA ON
TypeA.ID1 = SingleType.ID1 AND
TypeA.ID2 = SingleType.ID2
编辑:更新了示例数据和查询以表明我正在对两列进行分组,而不仅仅是一列。
Sample data:
ID1 ID2 Num Type
---------------------
1 1 1 'A'
1 1 2 'A'
1 2 3 'A'
1 2 4 'A'
2 1 1 'A'
2 2 1 'B'
3 1 1 'A'
3 2 1 'A'
Desired result:
ID1 ID2
---------
1 1
1 2
3 1
3 2
Notice that I'm grouping by ID1 and ID2, but not Num, and that I'm looking specifically for groups where Type = 'A'. I know it's doable through a join two queries on the same table: one query to find all groups that have a distinct Type, and another query to filter rows with Type = 'A'. But I was wondering if this can be done in a more efficient way.
I'm using SQL Server 2008, and my current query is:
SELECT ID1, ID2
FROM (
SELECT ID1, ID2
FROM T
GROUP BY ID1, ID2
HAVING COUNT( DISTINCT Type ) = 1
) AS SingleType
INNER JOIN (
SELECT ID1, ID2
FROM T
WHERE Type = 'A'
GROUP BY ID1, ID2
) AS TypeA ON
TypeA.ID1 = SingleType.ID1 AND
TypeA.ID2 = SingleType.ID2
EDIT: Updated sample data and query to indicate that I'm grouping on two columns, not just one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种不需要聚合(但确实需要不同)
ANTI-JOIN
的替代方案,请参阅它的工作原理9132209 的 data.se 查询示例(反连接)
不存在
看看它在这个9132209 的 data.se 查询示例不存在
There are two alternatives that don't require the aggregation (but do require distinct)
ANTI-JOIN
See it working at this data.se query Sample for 9132209 (Anti-Join)
NOT EXISTS
See it working at this data.se query Sample for 9132209 Not Exists