Complete the code to calculate the running total of sales ordered by date.
SELECT date, sales, SUM(sales) OVER (ORDER BY [1]) AS running_total FROM sales_data;The running total should be ordered by the date column to accumulate sales over time.
Complete the code to assign a rank to each employee based on their sales within each region.
SELECT employee_id, region, sales, RANK() OVER (PARTITION BY [1] ORDER BY sales DESC) AS sales_rank FROM employee_sales;Partitioning by region groups employees so ranks are calculated within each region separately.
Fix the error in the code to calculate the difference between current and previous sales values ordered by date.
SELECT date, sales, sales - LAG([1]) OVER (ORDER BY date) AS sales_diff FROM sales_data;The LAG function should refer to the sales column to get the previous sales value for subtraction.
Fill both blanks to calculate the cumulative average sales per region ordered by date.
SELECT region, date, sales, AVG([1]) OVER (PARTITION BY [2] ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_avg FROM sales_data;
The cumulative average is calculated on the sales column, partitioned by region to group data correctly.
Fill all three blanks to calculate the difference between current sales and the average sales of the previous 3 days within each region.
SELECT region, date, sales, sales - AVG([1]) OVER (PARTITION BY [2] ORDER BY [3] ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING) AS diff_from_avg_prev_3_days FROM sales_data;
The average is calculated on sales, partitioned by region, ordered by date to consider the previous 3 days correctly.