View combining latest order per customer
SQL
Hard
0 views
Problem Description
Create view v_customer_latest_order that shows each customer with their latest order_id and date.
Output Format
View created
Constraints
Use window function
Official Solution
CREATE VIEW v_customer_latest_order AS SELECT customer_id, order_id, order_date FROM (SELECT o.customer_id, o.order_id, o.order_date, ROW_NUMBER() OVER (PARTITION BY o.customer_id ORDER BY o.order_date DESC, o.order_id DESC) AS rn FROM Orders o) t WHERE rn = 1;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!