Skip to content

2922. Market Analysis III

Approach 1: inDegrees

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WITH
  Sellers AS(
    SELECT
      Users.seller_id,
      COUNT(DISTINCT Orders.item_id) AS num_items
    FROM Users
    INNER JOIN Orders
      USING (seller_id)
    INNER JOIN Items
      USING (item_id)
    WHERE Items.item_brand != Users.favorite_brand
    GROUP BY 1
  ),
  RankedSellers AS (
    SELECT
      seller_id,
      num_items,
      RANK() OVER(ORDER BY num_items DESC) AS `rank`
    FROM Sellers
  )
SELECT seller_id, num_items
FROM RankedSellers
WHERE `rank` = 1;

Approach 2: Heuristic