识别 SQL Server 中的链接 ID 集
我有一个简单的表,如下所示:
ClientID ItemID
1 1
1 2
1 3
2 1
2 2
3 3
4 3
5 1
5 2
5 4
5 5
其中两列组合为主键。我现在的任务是识别分配给 ClientID 的所有唯一的 ItemID 集。因此,在我的示例中,集合将是:
ItemIDs 1,2,3 (used by ClientID 1)
ItemIDs 1,2 (used by ClientID 2)
ItemIDs 3 (used by ClientIDs 3 and 4)
ItemIDs 1,2,4,5 (used by ClientID 5)
理想情况下,输出将是两个表:
SetID ItemID
1 1
1 2
1 3
2 1
2 2
3 3
4 1
4 2
4 4
4 5
ClientID SetID
1 1
2 2
3 3
4 3
5 4
其中 SetID 将是在其他地方使用的新字段。
目前,我识别唯一集的方法包括使用游标为每个 ClientID 构建有序 ItemID 的字符串,然后比较输出以获取唯一字符串,最后将其解析回来。写得很快,但感觉很糟糕。
我确信一定有比这更好的方法。有什么想法吗?
I have a simple table that looks like this:
ClientID ItemID
1 1
1 2
1 3
2 1
2 2
3 3
4 3
5 1
5 2
5 4
5 5
where both columns combine to be the primary key. I am now tasked with identifying all the unique sets of ItemIDs assigned to ClientIDs. So in my example, the sets would be:
ItemIDs 1,2,3 (used by ClientID 1)
ItemIDs 1,2 (used by ClientID 2)
ItemIDs 3 (used by ClientIDs 3 and 4)
ItemIDs 1,2,4,5 (used by ClientID 5)
Ideally the output would be two tables:
SetID ItemID
1 1
1 2
1 3
2 1
2 2
3 3
4 1
4 2
4 4
4 5
ClientID SetID
1 1
2 2
3 3
4 3
5 4
where SetID would be a new field for use elsewhere.
Currently the way I have of identifying the unique sets involves using a cursor to build a string of the ordered ItemIDs for each ClientID, then comparing the output to get the unique strings, and finally parsing it back. It was quick enough to write but feels horrible.
I'm sure there must be a better way than this. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果:
SetID 的值与您提供的输出中的值不完全相同,但我认为这不是一个大问题。 SetID 是根据按 ItemIDSet 排序的排名函数
rank() over(order by ItemIDSet)
生成的。拿它来旋转。
Result:
The values of SetID's is not exactly the same as in the output you have provided but I don't think that would be a big issue. The SetID's are generated from the rank function
rank() over(order by ItemIDSet)
ordered by ItemIDSet.Take it for a spin.