Complete the code to calculate the running total of sales using a window function.
SELECT sales, [1](sales) OVER (ORDER BY date) AS running_total FROM sales_data;The SUM function calculates the running total of sales when used as a window function with an ORDER BY clause.
Complete the code to calculate the average sales per department using a window function.
SELECT department, sales, AVG(sales) OVER (PARTITION BY [1]) AS avg_sales FROM sales_data;The PARTITION BY department clause groups rows by department to calculate the average sales per department.
Fix the error in the code to count the number of sales entries per region using a window function.
SELECT region, sales, COUNT([1]) OVER (PARTITION BY region) AS sales_count FROM sales_data;Using COUNT(*) counts all rows per region regardless of NULLs in any column.
Fill both blanks to calculate the cumulative average sales ordered by date within each region.
SELECT region, date, sales, AVG(sales) OVER (PARTITION BY [1] ORDER BY [2] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_avg FROM sales_data;
Partition by region and order by date to calculate cumulative average sales over time within each region.
Fill all three blanks to calculate the count of sales, sum of sales, and average sales per department.
SELECT department, COUNT([1]) OVER (PARTITION BY department) AS sales_count, SUM([2]) OVER (PARTITION BY department) AS sales_sum, AVG([3]) OVER (PARTITION BY department) AS sales_avg FROM sales_data;
Use COUNT(*) to count all rows, and SUM(sales) and AVG(sales) to calculate sum and average of sales per department.