SQL 上新商品首周销量:时间窗口筛选+聚合(SHEIN面试题)
一、题目
SHEIN每天都有大量新品上架,需要统计每个上新商品在上架后首周(7天内)的累计销量,按上架时间升序输出,以评估新品的市场表现。
假设有两张表:
t4_product_launch:商品上架时间表t4_order_detail:订单明细表
-- t4_product_launch 商品上架表
+---------+-------------------+
| sku_id | launch_time |
+---------+-------------------+
| SKU001 | 2025-06-01 08:00 |
| SKU002 | 2025-06-03 10:00 |
| SKU003 | 2025-06-05 09:00 |
+---------+-------------------+
-- t4_order_detail 订单明细表
+-----------+---------+------+-------------------+
| order_id | sku_id | qty | order_time |
+-----------+---------+------+-------------------+
| 1001 | SKU001 | 2 | 2025-06-02 10:00 |
| 1002 | SKU001 | 1 | 2025-06-05 14:00 |
| 1003 | SKU001 | 3 | 2025-06-09 09:00 |
| 1004 | SKU002 | 2 | 2025-06-04 10:00 |
| 1005 | SKU002 | 1 | 2025-06-08 16:00 |
| 1006 | SKU002 | 5 | 2025-06-12 11:00 |
| 1007 | SKU003 | 1 | 2025-06-06 08:00 |
| 1008 | SKU003 | 1 | 2025-06-14 12:00 |
+-----------+---------+------+-------------------+
二、思路分析
JOIN两表,筛选订单时间在上架后7天内的记录:datediff(order_time, launch_time) between 0 and 6;- 按SKU分组汇总首周销量;
- 注意保留那些首周0销量的新品(使用
LEFT JOIN)。
| 维度 | 评分 |
|---|---|
| 题目难度 | ⭐️⭐️⭐️ |
| 题目清晰度 | ⭐️⭐️⭐️⭐️⭐️ |
| 业务常见度 | ⭐️⭐️⭐️⭐️⭐️ |
三、逐步推导
1.筛选首周内的订单
关联两表,使用datediff筛选上架后0-6天内的订单记录:
select l.sku_id,
l.launch_time,
o.order_id,
o.qty,
o.order_time,
datediff(o.order_time, l.launch_time) as days_from_launch
from t4_product_launch l
left join t4_order_detail o
on l.sku_id = o.sku_id
and datediff(o.order_time, l.launch_time) between 0 and 6
查询结果
+---------+-------------------+-----------+------+-------------------+-------------------+
| sku_id | launch_time | order_id | qty | order_time | days_from_launch |
+---------+-------------------+-----------+------+-------------------+-------------------+
| SKU001 | 2025-06-01 08:00 | 1002 | 1 | 2025-06-05 14:00 | 4 |
| SKU001 | 2025-06-01 08:00 | 1001 | 2 | 2025-06-02 10:00 | 1 |
| SKU002 | 2025-06-03 10:00 | 1005 | 1 | 2025-06-08 16:00 | 5 |
| SKU002 | 2025-06-03 10:00 | 1004 | 2 | 2025-06-04 10:00 | 1 |
| SKU003 | 2025-06-05 09:00 | 1007 | 1 | 2025-06-06 08:00 | 1 |
+---------+-------------------+-----------+------+-------------------+-------------------+
5 rows selected (0.477 seconds)(https://www.dwsql.com)
2.汇总每个SKU的首周销量
按sku分组聚合首周销量,coalesce将无销量的新品补0,按上架时间升序输出:
select l.sku_id,
l.launch_time,
coalesce(sum(o.qty), 0) as first_week_sales
from t4_product_launch l
left join t4_order_detail o
on l.sku_id = o.sku_id
and datediff(o.order_time, l.launch_time) between 0 and 6
group by l.sku_id, l.launch_time
order by l.launch_time asc
查询结果
+---------+-------------------+-------------------+
| sku_id | launch_time | first_week_sales |
+---------+-------------------+-------------------+
| SKU001 | 2025-06-01 08:00 | 3 |
| SKU002 | 2025-06-03 10:00 | 3 |
| SKU003 | 2025-06-05 09:00 | 1 |
+---------+-------------------+-------------------+
3 rows selected (1.349 seconds)(https://www.dwsql.com)
四、常见坑点
坑1:时间窗口边界 — datediff between 0 and 6 表示首周(第0天=上架当天到第6天=第7天),注意0-index边界,若需求为上架后7天(不含当天)则应使用 between 1 and 7。
坑2:零销量新品 — left join 确保首周零销量的新品不丢失,coalesce(sum(qty), 0) 将 null 补为 0。
坑3:join性能 — datediff 在 join 条件中可能影响性能,大数据量下建议先将订单按日期范围预筛选,再与上架表 join。
五、知识点总结
| 考点 | 说明 |
|---|---|
| 多表JOIN | LEFT JOIN保留主表所有数据,COALESCE处理NULL |
| GROUP BY + 聚合函数 | 分组聚合计算首周累计销量 |
| 日期函数 | datediff计算时间差,between筛选时间窗口 |
| NULL值处理 | coalesce补0,确保无销量新品不丢失 |
六、建表语句
点击展开 DDL & DML
-- 商品上架表:记录每个sku的上架时间
create table t4_product_launch (
sku_id string comment 'sku编码',
launch_time string comment '上架时间'
) comment '商品上架时间表';
-- 订单明细表:记录每笔订单的购买明细
create table t4_order_detail (
order_id bigint comment '订单id',
sku_id string comment 'sku编码',
qty int comment '购买数量',
order_time string comment '下单时间'
) comment '订单明细表';
-- 插入商品上架数据
insert into t4_product_launch(sku_id, launch_time) values
('SKU001', '2025-06-01 08:00'),
('SKU002', '2025-06-03 10:00'),
('SKU003', '2025-06-05 09:00');
-- 插入订单明细数据
insert into t4_order_detail(order_id, sku_id, qty, order_time) values
(1001, 'SKU001', 2, '2025-06-02 10:00'),
(1002, 'SKU001', 1, '2025-06-05 14:00'),
(1003, 'SKU001', 3, '2025-06-09 09:00'),
(1004, 'SKU002', 2, '2025-06-04 10:00'),
(1005, 'SKU002', 1, '2025-06-08 16:00'),
(1006, 'SKU002', 5, '2025-06-12 11:00'),
(1007, 'SKU003', 1, '2025-06-06 08:00'),
(1008, 'SKU003', 1, '2025-06-14 12:00');
📱关注公众号
「数据仓库技术」文章同步更新,不错过每一篇干货

💬加群交流
备注「数据仓库技术」加入社群,每日一道大厂SQL真题
