题解 | 下单复盘

下单复盘

https://www.nowcoder.com/practice/85cece6c8e11434783e9e18da2bddd45

with feb as (
    select * from orders
    where date_format(order_date, "%Y-%m") = '2024-02'
)
select c.customer_id, customer_name, 
    ifnull(count(distinct f.order_id), 0) as feb_2024_order_count,
    ifnull(sum(qty*price), 0) as feb_2024_total_amount, 
    round(ifnull(sum(qty*price) / ifnull(count(distinct f.order_id), 0), 0), 2) as feb_2024_avg_order_amount,
    min(order_date) as feb_2024_first_order_date,
    max(order_date) as feb_2024_last_order_date
from customers c
left join feb f on c.customer_id = f.customer_id 
left join order_items o on f.order_id = o.order_id
group by c.customer_id, customer_name
order by feb_2024_total_amount desc, c.customer_id asc

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-10 11:42
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务