筛选去年总销量小于100的商品
题目需求
从订单明细表(order_detail)中筛选出去年总销量小于100的商品及其销量,假设今天的日期是2022-01-10,不考虑上架时间小于一个月的商品,期望结果如下:
sku_id (商品id) |
name (商品名称) |
order_num (销量) |
1 |
xiaomi 10 |
51 |
3 |
apple 12 |
36 |
4 |
xiaomi 13 |
53 |
6 |
洗碗机 |
32 |
select t1.sku_id sku_id, name, order_num from ( select sku_id, sum(sku_num) order_num from order_detail1 od where year(create_date) = '2021' group by sku_id ) t1 join sku_info1 si on t1.sku_id = si.sku_id where datediff('2022-01-10',si.from_date) > 30 and order_num < 100
查询每日新用户数
题目需求
从用户登录明细表(user_login_detail)中查询每天的新增用户数,若一个用户在某天登录了,且在这一天之前没登录过,则任务该用户为这一天的新增用户。期望结果如下:
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 |
select login_date_first,count(user_id) user_count from (select user_id,login_date_first, row_number() over (partition by user_id order by login_date_first) rn from (select user_id,date_format(login_ts,'yyyy-MM-dd') login_date_first from user_login_detail1)t1)t2 where rn=1 group by login_date_first
统计每个商品的销量最高的日期
题目需求
从订单明细表(order_detail)中统计出每种商品销售件数最多的日期及当日销量,如果有同一商品多日销量并列的情况,取其中的最小日期。期望结果如下:
sku_id(商品id) |
create_date(销量最高的日期) |
sum_num(销量) |
1 |
2021-10-02 |
9 |
2 |
2021-10-04 |
60 |
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 |
2021-10-08 |
59 |
9 |
2021-10-01 |
45 |
10 |
2021-10-08 |
94 |
11 |
2021-10-08 |
95 |
12 |
2021-10-08 |
83 |
select sku_id,create_date,sum_num from (select sku_id,create_date,sum_num,row_number() over (partition by sku_id order by sum_num desc,create_date asc ) rk from (select sku_id,sum(sku_num) sum_num, create_date from order_detail1 group by sku_id, create_date)t1)t2 where rk=1