使用MSSQL在一次交易中获取N个最常购买的组合产品

Get N most frequently bought combination product in one transaction using MSSQL

我有如下数据集:

transaction_id store_id product_id product_name
1 100 p001 product_1
1 100 p002 product_2
1 100 p003 product_3
4 100 p002 product_2
4 100 p003 product_3
5 100 p002 product_2
5 100 p003 product_3
7 100 p001 product_1
7 100 p003 product_3
8 101 p002 product_2
8 101 p003 product_3
9 101 p001 product_1
9 101 p002 product_2
2 101 p001 product_1
3 101 p002 product_2
3 101 p003 product_3
6 101 p001 product_1
6 101 p002 product_2

我正试图找到一个查询来给出如下输出。

store freq_prod_ids count_of_transactions
100 p002, p003 3
100 p001, p003 2
101 p001, p002 2
101 p002, p003 2

这基本上应该给出每个商店单笔交易中前 2 [N=2] 个最常购买的产品组合。

请帮助进行 SQL 查询以获得此响应。

您可以尝试以下方法,在基于商店和产品对进行聚合之前执行自连接。 row_number 用于检索每个商店的前 2 个产品对。

SELECT
    store_id, freq_prod_ids,count_of_transactions
FROM (
    SELECT
        t1.store_id,
        CONCAT(t1.product_id,', ',t2.product_id) as freq_prod_ids,
        COUNT(1) as count_of_transactions,
        ROW_NUMBER() OVER (PARTITION BY t1.store_id ORDER BY COUNT(1) DESC) as rn
    FROM my_table t1 
    INNER JOIN my_table t2 on t1.store_id = t2.store_id and 
                              t1.product_id < t2.product_id and
                              t1.transaction_id = t2.transaction_id
    GROUP BY t1.store_id,CONCAT(t1.product_id,', ',t2.product_id)
) t3 WHERE rn <=2

View working demo db fiddle

让我知道这是否适合你。