加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 06:34:40 所属栏目:MsSql教程 来源:网络整理
导读:我想创建一个频率分布矩阵 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 p12. Then I have to count the number of products that come together the most
我想创建一个频率分布矩阵
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;

您可以使用更多连接将其扩展到两个以上的产品.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读