[英]Identifying equivalent sets in SQL Server
Following is a sample of the records that I have in a table.以下是我在表中的记录示例。
EntriesPerSet
column contains the number of records in the table for that SET_ID
. EntriesPerSet
列包含表中该SET_ID
的记录数。 I need to establish equivalence between rows belonging to different SET_ID
s.我需要在属于不同
SET_ID
的行之间建立等效性。 Sets will be equivalent, if and only if they contain an equal number of entries per Set, and every entry has a corresponding entry in the other set (by checking values in K1
& K2
).集合将是等效的,当且仅当它们每个集合包含相同数量的条目,并且每个条目在另一个集合中都有一个对应的条目(通过检查
K1
和K2
值)。 In following case, SET_IDs
1 and 2 are equivalent.在以下情况下,
SET_IDs
1 和 2 是等效的。
SET_ID K1 K2 EntriesPerSet
1 a b 4
1 c d 4
1 e f 4
1 g h 4
2 a b 4
2 c d 4
2 e f 4
2 g h 4
3 a b 5
3 c d 5
3 e f 5
3 g h 5
3 i j 5
4 a b 3
4 c d 3
4 e f 3
5 a b 4
5 c d 4
5 e f 4
5 p q 4
Please help me with how to do this.请帮助我如何做到这一点。 Thanks!
谢谢!
If you just want to know which pairs are equivalent, you can just use a common table expression to get all possible combinations, and an INTERSECT
to figure out which of them overlap fully;如果你只是想知道哪些对是等价的,你可以使用一个公共表表达式来获得所有可能的组合,并使用一个
INTERSECT
来找出它们中哪些完全重叠;
WITH cte AS (
SELECT DISTINCT a.SET_ID aid, b.SET_ID bid, a.EntriesPerSet
FROM mysets a
JOIN mysets b ON a.EntriesPerSet = b.EntriesPerSet AND a.SET_ID < b.SET_ID
)
SELECT aid, bid FROM cte
WHERE EntriesPerSet = (
SELECT COUNT(*) FROM (
SELECT K1,K2 FROM mysets WHERE SET_ID=aid
INTERSECT
SELECT K1,K2 FROM mysets WHERE SET_ID=bid
) a
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.