Debunking Window Functions
A retail company have a employee_sales table which logs the sales of every employee in every city he is working for every department and for each month.
The sales head wants to get a couple of reports:
For the first report, one just has to group each department and then calculate the sum of sales_amount where the month is January.
SELECT dept, SUM(sales_amount) AS total_sales
FROM employee_sales
WHERE month = 'January'
GROUP BY dept;
But the second report is a bit complex. The boss wants to know per department who has made the highest sales.
In that case:
领英推荐
In the above table, we can see what happens to the original table when we window(group) it by dept and the sort the resulting window in descending order by sales_amount. In this case, we use ROW_NUMBER() window function to number each resulting row starting from 1 and the row with highest rank is the best performer. Yay!
WITH january_sales AS (
SELECT emp_id, dept, sales_amount,
ROW_NUMBER() OVER (PARTITION BY dept ORDER BY SUM(sales) DESC) as rank
FROM employee_sales
WHERE month = 'January')
SELECT emp_id, dept, sales_amount
FROM january_sales
WHERE rank = 1;
While for generating the first report, aggregate function is applied on the full table, whereas, for the second report, firs the table is grouped into windows and then the highest from each of the groups is selected.
There are several types of window functions in SQL, typically falling into the following categories: