SQL - 计算每个 Field-A 值的 Field-B 值集

发布于 2024-08-31 06:41:49 字数 450 浏览 2 评论 0原文

首先抱歉,我想不出更具描述性的标题。

我想要做的是仅使用 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 技术交流群。

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

发布评论

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

评论(3

金兰素衣 2024-09-07 06:41:49

在 MySQL 中,如果您有以下列表:

list1 = ('1', '3')
list2 = ('2', '3')
list3 = ('3', '5')

那么您可以执行以下操作:

SELECT
    TransactionID,
    SUM(ItemID IN ('1', '3')) AS list1_count,
    SUM(ItemID IN ('2', '3')) AS list2_count,
    SUM(ItemID IN ('3', '5')) AS list3_count
FROM table1
GROUP BY TransactionID
HAVING list1_count > 0 AND list2_count > 0 AND list3_count > 0

结果:

TransactionId  list1_count  list2_count  list3_count
1              3            2            1
3              2            2            1

测试数据:

CREATE TABLE table1 (ID INT NOT NULL, TransactionID INT NOT NULL, ItemID INT NOT NULL);
INSERT INTO table1 (ID, TransactionID, ItemID) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 1, 3),
(4, 1, 4),
(5, 1, 1),
(6, 2, 1),
(7, 2, 2),
(8, 2, 1),
(9, 2, 4),
(10, 3, 3),
(11, 3, 2),
(12, 3, 1);

In MySQL if you have the following lists:

list1 = ('1', '3')
list2 = ('2', '3')
list3 = ('3', '5')

then you can do this:

SELECT
    TransactionID,
    SUM(ItemID IN ('1', '3')) AS list1_count,
    SUM(ItemID IN ('2', '3')) AS list2_count,
    SUM(ItemID IN ('3', '5')) AS list3_count
FROM table1
GROUP BY TransactionID
HAVING list1_count > 0 AND list2_count > 0 AND list3_count > 0

Result:

TransactionId  list1_count  list2_count  list3_count
1              3            2            1
3              2            2            1

Test data:

CREATE TABLE table1 (ID INT NOT NULL, TransactionID INT NOT NULL, ItemID INT NOT NULL);
INSERT INTO table1 (ID, TransactionID, ItemID) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 1, 3),
(4, 1, 4),
(5, 1, 1),
(6, 2, 1),
(7, 2, 2),
(8, 2, 1),
(9, 2, 4),
(10, 3, 3),
(11, 3, 2),
(12, 3, 1);
心意如水 2024-09-07 06:41:49

根据您的方言,并假设您的列表是其他表......

SELECT
    TransactionID, Count1, Count2, Count3
FROM
    MyDataSet M
    JOIN
    (SELECT COUNT(*), ItemID AS Count1 FROM List1 GROUP BY ItemID) T1 ON T1.ItemID = M.ItemID
    JOIN
    (SELECT COUNT(*), ItemID AS Count2 FROM List2 GROUP BY ItemID) T2 ON T2.ItemID = M.ItemID
    JOIN
    (SELECT COUNT(*), ItemID AS Count3 FROM List3 GROUP BY ItemID) T3 ON T3.ItemID = M.ItemID

Depending on your dialect, and assuming your lists are other tables...

SELECT
    TransactionID, Count1, Count2, Count3
FROM
    MyDataSet M
    JOIN
    (SELECT COUNT(*), ItemID AS Count1 FROM List1 GROUP BY ItemID) T1 ON T1.ItemID = M.ItemID
    JOIN
    (SELECT COUNT(*), ItemID AS Count2 FROM List2 GROUP BY ItemID) T2 ON T2.ItemID = M.ItemID
    JOIN
    (SELECT COUNT(*), ItemID AS Count3 FROM List3 GROUP BY ItemID) T3 ON T3.ItemID = M.ItemID
糖粟与秋泊 2024-09-07 06:41:49

如果 list1、list2 和 list3 实际上是已知的枚举,您可以这样做:

SELECT TransactionID, COUNT(*)
FROM MyTable
WHERE ItemID IN (list1) AND ItemID IN (list2) AND ItemID IN (list3)
GROUP BY TransactionID

如果您有很多列表,您可能希望在程序中生成 SQL。然而,即使对于很多列表,它仍然应该表现得很好。将您期望匹配数最少的列表放在前面,以便您尽快停止评估谓词。

如果您的列表位于另一个表中,可能是一堆 (list_id, item_id) 形式的元组,那么这是一个更棘手的问题。在尝试提出查询之前,我想了解更多信息。

If list1, list2, and list3 are actually known enumerations, you could go with:

SELECT TransactionID, COUNT(*)
FROM MyTable
WHERE ItemID IN (list1) AND ItemID IN (list2) AND ItemID IN (list3)
GROUP BY TransactionID

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文