-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10-supercloud-customer.sql
40 lines (28 loc) · 1007 Bytes
/
10-supercloud-customer.sql
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
-- A Microsoft Azure Supercloud customer is defined as a company that purchases at least one product from each product category.
-- Write a query that effectively identifies the company ID of such Supercloud customers.
-- Solution :
WITH supercloud_customers AS (
SELECT customer_id
FROM (
SELECT customer_id,
COUNT(DISTINCT product_category) AS category_count
FROM customer_contracts cc JOIN products p USING (product_id)
GROUP BY 1
) x
WHERE category_count = (SELECT COUNT(DISTINCT product_category)
FROM products)
)
SELECT customer_id
FROM supercloud_customers
-- my approach: wasn't sure on how to add "category_count".
WITH supercloud_customers as (
SELECT customer_id,
product_category,
count(product_id)
FROM customer_contracts cc JOIN products p USING(product_id)
GROUP BY 1, 2
ORDER BY 1, 2
)
SELECT customer_id
FROM supercloud_customers
-- remarks: