select
customer_id,
customer_name,
product_name as latest_order
from
(select
customer_id,customer_name,product_name,
row_number() over(partition by customer_id
order by order_date desc)as rk
from
(select
o.customer_id,customer_name,product_name,order_date
from orders o
join customers c on o.customer_id=c.customer_id
join products p on o.product_id=p.product_id
group by 1,2,3,4)t)t1
where rk=1
order by customer_id;



京公网安备 11010502036488号