Lead/lag for day-to-day change

Lead/lag for day-to-day change

Medium SQL Functions 24 views
Explanation Complexity

Problem Statement

For daily sales, show today_sales and change_from_yesterday.

Input Format

SQL query

Output Format

Result set

Example

Orders(order_date, total_amount)
Daily rows with change

Constraints

Use LAG

Input / Output Format

Input Format
SQL query
Output Format
Result set
Constraints
Use LAG

Examples

Input:
Orders(order_date, total_amount)
Output:
Daily rows with change

Example Solution (Public)

SQL
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;

Official Solution Code

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;
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.