SQL需求:计算客户近30天消费额及最高消费商品
问题内容
现有数据表
transaction_details表
| 交易ID(transaction_id) | 客户ID(customer_id) | 商品ID(item_id) | 购买数量(item_number) | 交易时间(transaction_dttm) |
|---|---|---|---|---|
| 7765 | 1 | 23 | 1 | 2022-01-15 |
| 1254 | 2 | 12 | 4 | 2022-02-03 |
| 3332 | 3 | 56 | 2 | 2022-02-15 |
| 7658 | 1 | 43 | 1 | 2022-03-01 |
| 7231 | 4 | 56 | 1 | 2022-01-15 |
| 7231 | 2 | 23 | 2 | 2022-01-29 |
dict_item_prices表
| 商品ID(item_id) | 商品名称(item_name) | 商品价格(item_price) | 生效起始日期(valid_from_dt) | 生效截止日期(valid_to_dt) |
|---|---|---|---|---|
| 23 | 手机1 | 1000 | 2022-01-01 | 2022-12-31 |
| 12 | 笔记本电脑 | 5000 | 2022-01-02 | 2022-12-31 |
| 56 | 杯子 | 50 | 2022-01-02 | 2022-12-31 |
| 43 | 眼镜 | 700 | 2022-01-01 | 2022-12-31 |
需求说明
计算客户在报表生成当日起近30天的消费金额,并找出该客户在此期间消费金额最高的商品名称。计算时需关联dict_item_prices表中交易发生时有效的商品价格,未在此期间产生交易的客户不纳入最终结果。
示例结果
| 客户ID(customer_id) | 近30天消费额(amount_spent_lm) | 最高消费商品(top_item_lm) |
|---|---|---|
| 1 | 700 | 眼镜 |
| 2 | 20000 | 笔记本电脑 |
| 3 | 100 | 杯子 |
内容的提问来源于stack exchange,提问作者useless
相关产品推荐
相关产品推荐

