表: Users
+----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | join_date | date | | favorite_brand | varchar | +----------------+---------+ user_id 是此表主键(具有唯一值的列)。 表中描述了购物网站的用户信息,用户可以在此网站上进行商品买卖。
表: Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | order_date | date | | item_id | int | | buyer_id | int | | seller_id | int | +---------------+---------+ order_id 是此表主键(具有唯一值的列)。 item_id 是 Items 表的外键(reference 列)。 (buyer_id,seller_id)是 User 表的外键。
表:Items
+---------------+---------+ | Column Name | Type | +---------------+---------+ | item_id | int | | item_brand | varchar | +---------------+---------+ item_id 是此表的主键(具有唯一值的列)。
编写解决方案找出每个用户的注册日期和在 2019 年作为买家的订单总数。
以 任意顺序 返回结果表。
查询结果格式如下。
示例 1:
输入: Users 表: +---------+------------+----------------+ | user_id | join_date | favorite_brand | +---------+------------+----------------+ | 1 | 2018-01-01 | Lenovo | | 2 | 2018-02-09 | Samsung | | 3 | 2018-01-19 | LG | | 4 | 2018-05-21 | HP | +---------+------------+----------------+ Orders 表: +----------+------------+---------+----------+-----------+ | order_id | order_date | item_id | buyer_id | seller_id | +----------+------------+---------+----------+-----------+ | 1 | 2019-08-01 | 4 | 1 | 2 | | 2 | 2018-08-02 | 2 | 1 | 3 | | 3 | 2019-08-03 | 3 | 2 | 3 | | 4 | 2018-08-04 | 1 | 4 | 2 | | 5 | 2018-08-04 | 1 | 3 | 4 | | 6 | 2019-08-05 | 2 | 2 | 4 | +----------+------------+---------+----------+-----------+ Items 表: +---------+------------+ | item_id | item_brand | +---------+------------+ | 1 | Samsung | | 2 | Lenovo | | 3 | LG | | 4 | HP | +---------+------------+ 输出: +-----------+------------+----------------+ | buyer_id | join_date | orders_in_2019 | +-----------+------------+----------------+ | 1 | 2018-01-01 | 1 | | 2 | 2018-02-09 | 2 | | 3 | 2018-01-19 | 0 | | 4 | 2018-05-21 | 0 | +-----------+------------+----------------+ 思路1:
1、我先把用户表和订单表左连接,条件只取 2019 年的订单,这样每个用户即使没下单也能保留;
2、然后用 sum(case when o.order_id is not null then 1 else 0 end) over(partition by u.user_id) 统计每个用户在 2019 年的订单数量,这里用窗口函数按用户分组累加;
3、最后用 distinct 保证每个用户只显示一行,输出用户 ID、注册日期和 2019 年订单总数。
思路2:
1、先把用户表和订单表左连接,这样每个用户都会出现,即使他 2019 年没下过单也不会漏掉;
2、然后再把订单对应的商品表连上,并且只考虑用户最喜欢品牌的商品;
3、接着用 count(case when 年份=2019 then 1 end) 来统计每个用户 2019 年购买自己喜欢品牌的订单数量;
4、最后按用户 ID 分组,得到每个用户和他的注册日期以及 2019 年的订单数,并按用户 ID 排序输出。
代码1:
select distinct
u.user_id buyer_id ,
to_char(u.join_date,'yyyy-mm-dd') join_date ,
sum(case when o.order_id is not null then 1 else 0 end) over(partition by u.user_id) orders_in_2019
from Users u
left join Orders o
on u.user_id = o.buyer_id and to_char(order_date,'yyyy') = '2019
代码2:
select
u.user_id as buyer_id,
to_char(u.join_date,'YYYY-MM-DD') join_date,
count(case when extract(year from o.order_date) = 2019 then 1 end) as orders_in_2019
from users u
left join orders o on u.user_id = o.buyer_id
left join items i
on o.item_id = i.item_id
and u.favorite_brand = i.item_brand
group by u.user_id, u.join_date
order by u.user_id;