SQL - 计算每个 Field-A 值的 Field-B 值集
首先抱歉,我想不出更具描述性的标题。
我想要做的是仅使用 SQL 执行以下操作:
我有一些字符串列表,list1、list2 和 list3。
我有一个数据集,其中包含两个有趣的列:A 和 B。A 列包含 TransactionID,B 列包含 ItemID。
当然,可以有多行共享相同的 TransactionID。
我需要捕获每个列表中至少有一个 ItemID 的交易(list1 AND list2 AND list3)。
我还需要计算每笔交易发生了多少次。 [编辑] 也就是说,计算每个 TransactionID 有多少个完整的 ItemID 集”,“完整集”是 list1 的任何元素与 list2 的任何元素与 list3 的任何元素
我希望这有足够的意义,也许我将能够以清晰的头脑更好地解释它,
提前致谢。
First of all sorry that I could not think of a more descriptive title.
What I want to do is the following using only SQL:
I have some lists of strings, list1, list2 and list3.
I have a dataset that contains two interesting columns, A and B. Column A contains a TransactionID and column B contains an ItemID.
Naturally, there can be multiple rows that share the same TransactionIDs.
I need to catch those transactions that have at least one ItemID in each and every list (list1 AND list2 AND list3).
I also need to count how many times does that happen for each transaction.
[EDIT] That is, count how many full sets of ItemIDs there are for each TransactionID", "Full Set" being any element of the list1 with any element of the list2 with any element of the list3
I hope that makes enough sense, perhaps I will be able to explain it better with a clear head.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 MySQL 中,如果您有以下列表:
那么您可以执行以下操作:
结果:
测试数据:
In MySQL if you have the following lists:
then you can do this:
Result:
Test data:
根据您的方言,并假设您的列表是其他表......
Depending on your dialect, and assuming your lists are other tables...
如果 list1、list2 和 list3 实际上是已知的枚举,您可以这样做:
如果您有很多列表,您可能希望在程序中生成 SQL。然而,即使对于很多列表,它仍然应该表现得很好。将您期望匹配数最少的列表放在前面,以便您尽快停止评估谓词。
如果您的列表位于另一个表中,可能是一堆 (list_id, item_id) 形式的元组,那么这是一个更棘手的问题。在尝试提出查询之前,我想了解更多信息。
If list1, list2, and list3 are actually known enumerations, you could go with:
If you have a lot of lists, you may want to generate the SQL in a program. However, it should still perform pretty well, even for a lot of lists. Put the lists you expect to have the fewest matches in first, so that you stop evaluating the predicate as soon as possible.
If your lists are in another table, perhaps a bunch of tuples of the form (list_id, item_id), that's a trickier problem. I'd like to know more before trying to come up with a query for that.