来自同一表的MySQL查询

kknvjkwl  于 2022-12-17  发布在  Mysql
关注(0)|答案(1)|浏览(143)

我希望我的这篇文章没有违反任何规则,我有一个表(在instacart数据库中)叫做order_products,它有order_id和product_id列,描述了每个订单中购买的产品。
下面是创建表

-- Name: order_products; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.order_products (
    order_id integer NOT NULL,
    product_id integer NOT NULL,
    add_to_cart_order integer,
    reordered integer
);

以下是所要求的数据示例

----------+------------+
| Order_id | Product_id |
+----------+------------+
| 123      | 741        |
+----------+------------+
| 123      | 742        |
+----------+------------+
| 123      | 852        |
+----------+------------+
| 234      | 852        |
+----------+------------+
| 234      | 963        |
+----------+------------+
| 456      | 741        |
+----------+------------+
| 456      | 742        |
+----------+------------+
| 456      | 201        |
+----------+------------+
| 456      | 202        |
+----------+------------+
| 567      | 741        |
+----------+------------+
| 567      | 742        |
+----------+------------+
| 567      | 201        |
+----------+------------+
| 789      | 963        |
+----------+------------+
| 789      | 201        |
+----------+------------+
| 789      | 202        |
+----------+------------+
| 789      | 203        |
+----------+------------+
| 789      | 204        |
+----------+------------+
| 100      | 741        |
+----------+------------+
| 100      | 741        |
+----------+------------+
| 100      | 201        |
+----------+------------+

我想查询在一个订单中一起订购次数最多的对。在上面的例子中,它将是产品ID 741和742对,因为它们在一个订单中一起订购了4次。
我基于自连接尝试了以下操作

SELECT p1.product_id, count(p1.product_id), p2.product_id, count(p2.product_id)

FROM order_products AS p1, order_products AS p2

WHERE p1.order_id = p2.order_id

GROUP BY p1.product_id, p2.product_id

ORDER BY 2 DESC

运气不好,因为它只返回购买次数最多的产品和数量的两倍,而我需要的是购买次数最多的一对产品在一起,我没有线索解决这个问题,有人能帮我吗?谢谢

tzdcorbm

tzdcorbm1#

可以按如下方式使用自联接和聚合:(此操作将退回一起购买的所有成对产品)

Select OP1.product_id As Product1,
       OP2.product_id As Product2,
       Count(*) As NumberOfOrders
From order_products OP1 Join order_products OP2
On OP1.order_id = OP2.order_id And 
   OP1.product_id > OP2.product_id
Group By OP1.product_id, OP2.product_id
Order By Count(*) Desc

如果您想退回 * 一起购买最多的一对产品 *,可以使用DENSE_RANK函数,如下所示:

Select Product1, Product2, NumberOfOrders
From
(
  Select OP1.product_id As Product1,
       OP2.product_id As Product2,
       Count(*) As NumberOfOrders,
       DENSE_RANK() Over (Order By Count(*) Desc) rnk
  From order_products OP1 Join order_products OP2
  On OP1.order_id = OP2.order_id And 
   OP1.product_id > OP2.product_id
  Group By OP1.product_id, OP2.product_id
) T
Where rnk = 1

参见demo

相关问题