0

我很难想象这一点,需要一些帮助。负责查找最常一起购买的物品。就像客户通常会在购买中添加什么一样。并且发生了多少次。

所有数据都在 1 个表中,包含以下列:

  • 订单号
  • 项目代码
  • 色标
  • 尺寸
  • 商品描述

此表中的每一行不一定是唯一的购买 - 例如,它可以列出两次订单号 1,因为他们购买了商品 A 和商品 B,因此有 2 行。

我知道这可能不是最好的解释,所以如果您有任何问题,请告诉我。

4

1 回答 1

1

您可以使用自联接和聚合获得两个项目所在的订单数:

select t1.itemcode, t2.itemcode, count(distinct t1.ordernumber) as num_orders
from t t1 join
     t t2
     on t1.ordernumber = t2.ordernumber and
        t1.itemcode < t2.itemcode
group by t1.itemcode, t2.itemcode
order by num_orders desc;
于 2020-01-30T23:25:20.563 回答