1045.买下所有产品的客户
| 序号 | 题目 | 难度 |
|---|---|---|
| 1045 | 题目链接 | 中等 |
题目大意
编写解决方案,报告 Customer 表中购买了 Product 表中所有产品的客户的 id。
返回结果表 无顺序要求 。
Customer 表:
| Column Name | Type |
|---|---|
| customer_id | int |
| product_key | int |
该表可能包含重复的行。 customer_id 不为 NULL。 product_key 是 Product 表的外键(reference 列)。
Product 表:
| Column Name | Type |
|---|---|
| product_key | int |
product_key 是这张表的主键(具有唯一值的列)。
示例
输入
Customer 表:
| customer_id | product_key |
|---|---|
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
Product 表:
| product_key |
|---|
| 5 |
| 6 |
输出
| customer_id |
|---|
| 1 |
| 3 |
解释
购买了所有产品(5 和 6)的客户的 id 是 1 和 3 。
参考答案
with t1 as (
select
customer_id
,count(distinct(product_key)) cnt
from Customer
group by 1
)
select
customer_id
from t1
where t1.cnt = (select count(1) from Product)
;