题解 | 下单最多的商品
下单最多的商品
https://www.nowcoder.com/practice/d7c93e3a3d5b4087896539121d32d367
# 【问题】需要计算订单下单最多的商品id,取第一个返回即可;注最早支付的订单是只需要考虑order_log中的订单号的订单,非整条支付链路;相同次数以product_id顺序排列
# 问题粒度:product_id商品id
# user_client_log 粒度:trace_id+step
# product_info 粒度:product_id
select
u.product_id
,sum(tmp_lab) as cnt
from
(
select
trace_id
,step
,case when step = 'order' then 1 else 0 end as tmp_lab
,product_id
from user_client_log
having step = 'order'
) u
left join product_info p
on u.product_id = p.product_id
group by u.product_id
order by cnt desc, product_id asc
limit 1
