期望结果如下:
sku_id |
year |
order_num |
order_amount |
1 |
2020 |
2 |
4000.00 |
2 |
2020 |
26 |
260.00 |
3 |
2020 |
1 |
5000.00 |
4 |
2021 |
53 |
318000.00 |
5 |
2021 |
242 |
121000.00 |
6 |
2020 |
6 |
12000.00 |
7 |
2020 |
35 |
3500.00 |
8 |
2020 |
59 |
35400.00 |
9 |
2021 |
194 |
194000.00 |
10 |
2020 |
94 |
9400.00 |
11 |
2020 |
95 |
4750.00 |
12 |
2020 |
83 |
1660.00 |
需要用到的表:
订单明细表:order_detail
order_detail_id(订单明细id) |
order_id(订单id) |
sku_id(商品id) |
create_date(下单日期) |
price(商品单价) |
sku_num(商品件数) |
1 |
1 |
1 |
2021-09-30 |
2000.00 |
2 |
2 |
1 |
3 |
2021-09-30 |
5000.00 |
5 |
22 |
10 |
4 |
2020-10-02 |
6000.00 |
1 |
23 |
10 |
5 |
2020-10-02 |
500.00 |
24 |
24 |
10 |
6 |
2020-10-02 |
2000.00 |
5 |
代码
with t as ( select sku_id ,substr(create_date,1,4) as year ,sum(sku_num) as order_num ,sum(price*sku_num) as order_amount ,rank() over (partition by sku_id order by substr(create_date,1,4) ) as rk from order_detail group by sku_id ,substr(create_date,1,4) ) select sku_id ,year ,order_num ,order_amount from t where rk =1
期望结果如下:
sku_id |
name |
order_num |
1 |
xiaomi 10 |
49 |
3 |
apple 12 |
35 |
4 |
xiaomi 13 |
53 |
6 |
洗碗机 |
26 |
需要用到的表:
商品信息表:sku_info
sku_id(商品id) |
name(商品名称) |
category_id(分类id) |
from_date(上架日期) |
price(商品价格) |
1 |
xiaomi 10 |
1 |
2020-01-01 |
2000 |
6 |
洗碗机 |
2 |
2020-02-01 |
2000 |
9 |
自行车 |
3 |
2020-01-01 |
1000 |
订单明细表:order_detail
order_detail_id(订单明细id) |
order_id(订单id) |
sku_id(商品id) |
create_date(下单日期) |
price(商品单价) |
sku_num(商品件数) |
1 |
1 |
1 |
2021-09-30 |
2000.00 |
2 |
2 |
1 |
3 |
2021-09-30 |
5000.00 |
5 |
22 |
10 |
4 |
2020-10-02 |
6000.00 |
1 |
23 |
10 |
5 |
2020-10-02 |
500.00 |
24 |
24 |
10 |
6 |
2020-10-02 |
2000 |
代码
with t as ( select a.sku_id ,b.name ,sum(a.sku_num) as order_num from order_detail a left join sku_info b on a.sku_id = b.sku_id where substr(a.create_date ,1,4)='2021' and b.from_date < date_add('2022-01-10',-30) group by a.sku_id ,b.name) select * from t where order_num < 100
期望结果如下:
login_date_first |
user_count |
2021-09-21 |
1 |
2021-09-22 |
1 |
2021-09-23 |
1 |
2021-09-24 |
1 |
2021-09-25 |
1 |
2021-09-26 |
1 |
2021-09-27 |
1 |
2021-10-04 |
2 |
2021-10-06 |
1 |
需要用到的表:
用户登录明细表: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 |
代码
with t as ( select user_id ,login_ts ,lag(login_ts,1,'null') over(partition by user_id order by login_ts ) as last_date from (select distinct user_id,substr(login_ts,1,10)as login_ts from user_login_detail)a) select login_ts as login_date_first ,sum(if(last_date='null',1,0)) as user_count from t where last_date ='null' group by login_ts
期望结果如下:
sku_id |
create_date |
sum_num |
1 |
2021-09-30 |
9 |
2 |
2021-10-02 |
5800 |
3 |
2021-10-05 |
9 |
4 |
2021-10-07 |
10 |
5 |
2021-10-03 |
47 |
6 |
2021-10-03 |
8 |
7 |
2021-10-05 |
58 |
8 |
2020-10-08 |
59 |
9 |
2021-10-01 |
45 |
10 |
2020-10-08 |
94 |
11 |
2020-10-08 |
95 |
12 |
2021-10-03 |
20400 |
需要用到的表:
订单明细表:order_detail
order_detail_id(订单明细id) |
order_id(订单id) |
sku_id(商品id) |
create_date(下单日期) |
price(商品单价) |
sku_num(商品件数) |
1 |
1 |
1 |
2021-09-30 |
2000.00 |
2 |
2 |
1 |
3 |
2021-09-30 |
5000.00 |
5 |
22 |
10 |
4 |
2020-10-02 |
6000.00 |
1 |
23 |
10 |
5 |
2020-10-02 |
500.00 |
24 |
24 |
10 |
6 |
2020-10-02 |
2000.00 |
5 |
代码
with t as ( select sku_id ,create_date ,sum(sku_num) as sum_num from order_detail group by sku_id ,create_date) select sku_id ,create_date ,sum_num from ( select * ,row_number() over (partition by sku_id order by sum_num desc ) as rk from t)a where rk=1
期望结果如下:
sku_id |
name |
sum_num |
cate_avg_num |
2 |
手机壳 |
6044 |
1546 |
5 |
破壁机 |
242 |
194 |
7 |
热水壶 |
252 |
194 |
8 |
微波炉 |
253 |
194 |
12 |
遮阳伞 |
20682 |
5373 |
需要用到的表:
商品信息表:sku_info
sku_id(商品id) |
name(商品名称) |
category_id(分类id) |
from_date(上架日期) |
price(商品价格) |
1 |
xiaomi 10 |
1 |
2020-01-01 |
2000 |
6 |
洗碗机 |
2 |
2020-02-01 |
2000 |
9 |
自行车 |
3 |
2020-01-01 |
1000 |
订单明细表:order_detail
order_detail_id(订单明细id) |
order_id(订单id) |
sku_id(商品id) |
create_date(下单日期) |
price(商品单价) |
sku_num(商品件数) |
1 |
1 |
1 |
2021-09-30 |
2000.00 |
2 |
2 |
1 |
3 |
2021-09-30 |
5000.00 |
5 |
22 |
10 |
4 |
2020-10-02 |
6000.00 |
1 |
23 |
10 |
5 |
2020-10-02 |
500.00 |
24 |
24 |
10 |
6 |
2020-10-02 |
2000.00 |
5 |
代码
with t1 as ( select a.sku_id ,b.name ,b.category_id ,sum(sku_num) as sum_num from order_detail a left join sku_info b on a.sku_id=b.sku_id group by a.sku_id ,b.name ,b.category_id ), t2 as ( select distinct b.category_id ,cast (sum(a.sku_num) over (partition by b.category_id )/count(distinct a.sku_id) over (partition by b.category_id ) as int ) as cate_avg_num from order_detail a left join sku_info b on a.sku_id=b.sku_id ) select t1.sku_id ,t1.name ,t1.sum_num ,t2.cate_avg_num from t1 left join t2 on t1.category_id=t2.category_id where t1.sum_num > t2.cate_avg_num
期望结果如下:
user_id |
register_date |
total_login_count |
login_count_2021 |
order_count_2021 |
order_amount_2021 |
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 |
代码
with t1 as ( select distinct user_id ,min(substr(login_ts,1,10)) over(partition by user_id rows between unbounded preceding and unbounded following ) as register_date ,count(login_ts) over(partition by user_id rows between unbounded preceding and unbounded following ) as total_login_count ,sum(if(substr(login_ts,1,4)='2021',1,0)) over(partition by user_id rows between unbounded preceding and unbounded following ) as login_count_2021 from user_login_detail ) ,t2 as ( select user_id ,count(order_id ) as order_count_2021 ,sum(total_amount ) as order_amount_2021 from order_info where substr(create_date,1,4)='2021' group by user_id ) select t1.user_id ,t1.register_date ,t1.total_login_count ,nvl(t1.login_count_2021,0) as login_count_2021 ,nvl(t2.order_count_2021,0) as order_count_2021 ,nvl(t2.order_amount_2021,0) as order_amount_2021 from t1 left join t2 on t1.user_id=t2.user_id order by t1.user_id