题解 | 下单复盘

下单复盘

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

select
    customer_id,
    customer_name,
    count(distinct order_id) feb_2024_order_count,
    round(coalesce(sum(qty*price),0),2) feb_2024_total_amount,
    round(coalesce(sum(qty*price)/nullif(count(distinct order_id),0),0),2) feb_2024_avg_order_amount,
    min(order_date) feb_2024_first_order_date,
    max(order_date) feb_2024_last_order_date
from
    customers s1
left join
    (select * from orders where date_format(order_date,'%Y%m') = 202402) s2
using(customer_id)
left join
    order_items s3
using(order_id)

group by 1,2
order by 4 desc,1 asc

业务里一般用coalesce代替ifnull

全部评论

相关推荐

牛客74464229...:年底就这样,招人的要不是很缺人的,要不就是岗位要求高的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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