gpt4 book ai didi

SQL查询产品的频率分布矩阵

转载 作者:行者123 更新时间:2023-12-04 03:56:41 24 4
gpt4 key购买 nike

我想创建一个频率分布矩阵

1.Create a matrix.**Is it possible to get this in separate columns**

customer1 p1 p2 p3
customer 2 p2 p3
customer 3 p2 p3 p1
customer 4 p2 p1

2. Then I have to count the number of products that come together the most

For eg
p2 and p3 comes together 3 times
p1 p3 comes 2 times
p1 p2 comes 2 times

I want to recommend products to customers ,frequency of products that comes together

select customerId,product,count(*) from sales group by customerId,product

谁能帮我解决这个问题

最佳答案

如果要客户购买的成对产品,则可以使用自我联接:

select s1.product, s2.product, count(*) as cnt
from sales s1 join
sales s2
on s1.customerId = s2.customerId
where s1.product < s2.product
group by s1.product, s2.product
order by cnt desc;

您可以通过使用更多联接将其扩展到两个以上的产品。

关于SQL查询产品的频率分布矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42749016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com