Skip to content

2324. Product Sales Analysis IV 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
WITH
  RankedSpending AS (
    SELECT
      Sales.user_id,
      Sales.product_id,
      RANK() OVER(
        PARTITION by user_id
        ORDER BY SUM(Sales.quantity * Product.price) DESC
      ) AS `rank`
    FROM Sales
    INNER JOIN Product
      USING (product_id)
    GROUP BY 1, 2
  )
SELECT user_id, product_id
FROM RankedSpending
WHERE `rank` = 1;