I have a love-hate relationship with the window functions in SQL.
I love how it allows one access to data from rows beyond the current row as it simplifies the tasks like creating rolling averages or ranking values within a group.
I hate it when I have to write a complicated query involving the usage of window functions along with joins, subqueries, CTEs, etc.
But I learned a new detail recently about window functions that made me gasp and admire it.
To understand it, let us consider the task of creating a 3-day rolling average on a numeric column.
This task requires data from the current row and the preceding two rows. Hence, it is a no-brainer to use window functions here.
The usual syntax of the window function for averaging is as below:
AVG() OVER ()
This window function considers the value of the current row and the value of all the preceding rows to calculate and display the average. (Let us overlook the functionalities of PARTITION BY and ORDER BY clauses for a while.)
But, to calculate the 3-day rolling average, we need this function to access only two preceding rows along with the current one.
This can be done by a simple addition as below:
AVG() OVER(ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
“ROWS BETWEEN” allows one to specify a range for the aggregation performed by window functions.
Its syntax is as below:
ROWS BETWEEN preceding_limit AND following_limit
If we specify these limits as “UNBOUNDED PRECEDING” or “UNBOUNDED FOLLOWING,” all the preceding and following rows are considered accordingly.
If we want to restrict these limits to a few rows, we could use below:
n PRECEDING (restricts to ’n’ preceding rows)
n FOLLOWING (restricts to ’n’ following rows)
If we want the aggregation to start from or end at the current row, simply specifying the corresponding limit as “CURRENT ROW” would suffice.
Default mode when we omit ‘ROWS BETWEEN’ from our queries is UNBOUNDED PRECEDING to CURRENT ROW, which explains the usual functionality of window functions as we know.
I wonder why I was in the dark about this functionality.
Comments
Post a Comment