标签:用户注册 user 09 Hive order 2021 login id 刷题
需求描述
从用户登录明细表(user_login_detail)和订单信息表(order_info)中查询每个用户的注册日期(首次登录日期)、总登录次数以及其在2021年的登录次数、订单数和订单总额。
期望结果如下:
user_id
(用户id)
|
register_date
(注册日期)
|
total_login_count
(累积登录次数)
|
login_count_2021
(2021年登录次数)
|
order_count_2021
(2021年下单次数)
|
order_amount_2021
(2021年订单金额)
<decimal(16,2)>
|
101
|
2021-09-21
|
5
|
5
|
4
|
143660.00
|
102
|
2021-09-22
|
4
|
4
|
4
|
177850.00
|
103
|
2021-09-23
|
2
|
2
|
4
|
75890.00
|
104
|
2021-09-24
|
4
|
4
|
4
|
89880.00
|
105
|
2021-10-04
|
1
|
1
|
4
|
120100.00
|
106
|
2021-10-04
|
2
|
2
|
4
|
119150.00
|
107
|
2021-09-25
|
4
|
4
|
4
|
124150.00
|
108
|
2021-10-06
|
2
|
2
|
4
|
155770.00
|
109
|
2021-09-26
|
3
|
3
|
2
|
129480.00
|
需要用到的表:
用户登录明细表:user_login_detail
user_id(用户id)
|
ip_address(ip地址)
|
login_ts(登录时间)
|
logout_ts(登出时间)
|
101
|
180.149.130.161
|
2021-09-21 08:00:00
|
2021-09-27 08:30:00
|
102
|
120.245.11.2
|
2021-09-22 09:00:00
|
2021-09-27 09:30:00
|
103
|
27.184.97.3
|
2021-09-23 10:00:00
|
2021-09-27 10:30:00
|
订单信息表:order_info
order_id
(订单id)
|
user_id
(用户id)
|
create_date
(下单日期)
|
total_amount
(订单金额)
|
1
|
101
|
2021-09-30
|
29000.00
|
10
|
103
|
2020-10-02
|
28000.00
|
参考SQL
select t.user_id,register_date,total_login_count,login_count_2021,order_count_2021,order_amount_2021
from (select user_id,
date(min(login_ts)) register_date,
sum(1) total_login_count,
sum(if(year(login_ts) = 2021, 1, 0)) login_count_2021
from user_login_detail
group by user_id) t
join
(select user_id,
sum(1) order_count_2021,
sum(total_amount) order_amount_2021
from order_info
where year(create_date) = 2021
group by user_id) t1 on t.user_id = t1.user_id;
标签:用户注册,
user,
09,
Hive,
order,
2021,
login,
id,
刷题
From: https://www.cnblogs.com/wdh01/p/17041033.html