-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3554. Find Category Recommendation Pairs.sql
More file actions
51 lines (45 loc) · 1.91 KB
/
3554. Find Category Recommendation Pairs.sql
File metadata and controls
51 lines (45 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Question 3554. Find Category Recommendation Pairs
Link: https://leetcode.com/problems/find-category-recommendation-pairs/description/?envType=problem-list-v2&envId=database
Table: ProductPurchases
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| product_id | int |
| quantity | int |
+-------------+------+
(user_id, product_id) is the unique identifier for this table.
Each row represents a purchase of a product by a user in a specific quantity.
Table: ProductInfo
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| category | varchar |
| price | decimal |
+-------------+---------+
product_id is the unique identifier for this table.
Each row assigns a category and price to a product.
Amazon wants to understand shopping patterns across product categories. Write a solution to:
Find all category pairs (where category1 < category2)
For each category pair, determine the number of unique customers who purchased products from both categories
A category pair is considered reportable if at least 3 different customers have purchased products from both categories.
Return the result table of reportable category pairs ordered by customer_count in descending order, and in case of a tie, by category1 in ascending order lexicographically, and then by category2 in ascending order.
*/
SELECT
p1.category AS category1,
p2.category AS category2,
COUNT(DISTINCT pp2.user_id) AS customer_count
FROM ProductInfo AS p1
CROSS JOIN ProductInfo AS p2
RIGHT JOIN --noqa: CV08
ProductPurchases AS pp1
ON p1.product_id = pp1.product_id
RIGHT JOIN --noqa: CV08
ProductPurchases AS pp2
ON p2.product_id = pp2.product_id AND pp1.user_id = pp2.user_id
WHERE p1.category < p2.category
GROUP BY p1.category, p2.category
HAVING COUNT(DISTINCT pp2.user_id) >= 3
ORDER BY customer_count DESC, p1.category ASC, p2.category ASC