Lead/lag for day-to-day change
SQL
Medium
3 views
Problem Description
For daily sales, show today_sales and change_from_yesterday.
Sample Test Case
Input:
Orders(order_date, total_amount)
Output:
Daily rows with change
Official Solution
SELECT order_date, total_sales, total_sales - LAG(total_sales) OVER (ORDER BY order_date) AS change_from_yesterday FROM (SELECT order_date, SUM(total_amount) AS total_sales FROM Orders GROUP BY order_date) d ORDER BY order_date;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!