1.题目
| order_id | user_id | product_id | quantity | purchase_time |
|----------|---------|------------|----------|---------------------|
| 1 | 1 | 1001 | 1 | 2023-03-13 08:30:00 |
| 2 | 1 | 1002 | 1 | 2023-03-13 10:45:00 |
| 3 | 1 | 1001 | 1 | 2023-03-13 10:45:01 |
| 4 | 2 | 1001 | 3 | 2023-03-13 14:20:00 |
| 5 | 3 | 1003 | 1 | 2023-03-13 16:15:00 |
| 6 | 3 | 1002 | 1 | 2023-03-13 12:10:00 |
| 7 | 3 | 1001 | 1 | 2023-03-13 12:10:01 |
| 8 | 4 | 1002 | 2 | 2023-03-13 09:00:00 |
| 9 | 4 | 1003 | 1 | 2023-03-13 11:30:00 |
| 10 | 4 | 1004 | 3 | 2023-03-13 13:40:00 |
| 11 | 4 | 1001 | 1 | 2023-03-13 17:25:00 |
| 12 | 4 | 1002 | 2 | 2023-03-13 15:05:00 |
| 13 | 4 | 1004 | 1 | 2023-03-13 11:55:00 |
2.题目分析:
3.SQL
step1:查询符合购买2个或者以上商品的用户
selectuser_idfrom(selectuser_id,product_id,sum(quantity) as product_u_numsfrom t_ordergroup by user_id,product_idhaving sum(quantity) >=2) tgroup by user_id;
查询结果

step2:计算每个用户的订单量
select user_id,count(order_id) as order_numfrom t_order t1group by user_id
查询结果

step3:关联step1和step2的结果,得出最后结果
selectt1.user_idfrom(selectuser_id,count(order_id) as order_numfrom t_order t1group by user_id) t1join(selectuser_idfrom(selectuser_id,product_id,sum(quantity) as product_u_numsfrom t_ordergroup by user_id,product_idhaving sum(quantity) >=2) tgroup by user_id) t2on t1.user_id = t2.user_idorder by t1.order_num desclimit 3
查询结果

4.数据准备
1.建表语句
CREATE TABLE t_order (order_id INT,user_id INT,product_id INT,quantity INT,purchase_time TIMESTAMP)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','STORED AS TEXTFILE;
2.数据插入语句
INSERT INTO t_order VALUES(1, 1, 1001, 1, '2023-03-13 08:30:00'),(2, 1, 1002, 1, '2023-03-13 10:45:00'),(3, 1, 1001, 1, '2023-03-13 10:45:01'),(4, 2, 1001, 3, '2023-03-13 14:20:00'),(5, 3, 1003, 1, '2023-03-13 16:15:00'),(6, 3, 1002, 1, '2023-03-13 12:10:00'),(7, 3, 1001, 1, '2023-03-13 12:10:01'),(8, 4, 1002, 2, '2023-03-13 09:00:00'),(9, 4, 1003, 1, '2023-03-13 11:30:00'),(10, 4, 1004, 3, '2023-03-13 13:40:00'),(11, 4, 1001, 1, '2023-03-13 17:25:00'),(12, 4, 1002, 2, '2023-03-13 15:05:00'),(13, 4, 1004, 1, '2023-03-13 11:55:00');
点点关注不迷路,天天学习有进步
分享↓收藏↓点赞↓再看↓
原文作者:数据仓库技术;如有侵权,请联系管理员删除。