最近做了京东的24年春招题,题目如下:
题目分析
本题要求统计2023年每个品牌的总销售额、总销量和平均客户满意度分数。
输出字段:
- brand_id(品牌ID)
- total_sales_amount(总销售额)
- total_sales_quantity(总销量)
- avg_satisfaction_score(平均满意度分数,保留两位小数)
输出顺序:题目未指定排序,默认按分组顺序输出。
涉及知识点:
- SQL 多表连接(JOIN)
- 条件筛选(WHERE + 时间区间)
- 分组聚合(SUM、AVG、GROUP BY)
- 字段别名与保留小数(ROUND)
解答步骤
1. 关联销售数据与客户反馈
- 用
sales_data
销售数据表与customer_feedback
客户反馈表通过销售ID(sales_id)连接,获取每笔销售的客户满意度分数。
from sales_data s
join customer_feedback c on c.sales_id = s.sales_id
2. 筛选2023年内的销售数据
- 只保留销售月份在2023年1月至12月之间的销售记录。
where sales_month between '2023-01' and '2023-12'
3. 按品牌分组统计
- 按品牌ID分组,统计:
- 总销售额:
sum(s.sales_amount)
- 总销量:
sum(s.sales_quantity)
- 平均满意度分数:
round(avg(c.satisfaction_score),2)
- 总销售额:
group by s.brand_id
完整代码
select s.brand_id,
sum(s.sales_amount) as total_sales_amount,
sum(s.sales_quantity) as total_sales_quantity,
round(avg(c.satisfaction_score),2) as avg_satisfaction_score
from sales_data s
join customer_feedback c on c.sales_id = s.sales_id
where sales_month between '2023-01' and '2023-12'
group by s.brand_id
近似题目练习推荐
- 知识点:子查询与分组聚合、分组统计、JOIN
- 知识点:分组聚合、SQL连接、聚合函数、排序
如需更多类似题目,可在牛客网SQL练习区进行练习。