This article is an extension of the window function, to follow up on each of the window functions.
The LISTAGG function will aggregate values that are collected within the window defined by the partition expression.
In this example, we’ll list each sale in the database; adding a column at the end which will list all of the salespeople who made a sale on that particular day.
This article is an extension of the window function, to follow up on each of the window functions.
The LEAD function returns values in an offset row. The offset is given by the second argument to the function itself. The offset pushes the index below (or after) the selected row.
To demonstrate the effect of this function, we’ll take the sales_id value, and then we’ll also take a led sales_id. By adjusting the offset value, you’ll see how the leaded value ascends up the rows.
This article is an extension of the window function, to follow up on each of the window functions.
The LAG function returns values in an offset row. The offset is given by the second argument to the function itself. The offset pushes the index above (or before) the selected row.
To demonstrate the effect of this function, we’ll take the sales_id value, and then we’ll also take a lagged sales_id. By adjusting the offset value, you’ll see how the lagged value slips down the rows.
Window functions allow database developers to perform analysis over partitions of information, very quickly. Prior to window functions, developers would need to create sub-queries (or common table expressions) that would allow their windows to be created. Primary queries would then work over the sub-queries to propgate a result. This now goes away, using the OVER syntax.
In today’s article we’ll walk through the basic structure of how to use window functions.
function (expression) OVER (
[ PARTITION BY expr_list ]
[ ORDER BY order_list [ frame_clause ] ] )
where function is one of the functions described in this section and expr_list is:
expression | column_name [, expr_list ]
and order_list is:
expression | column_name [ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
[, order_list ]
and frame_clause is:
ROWS
{ UNBOUNDED PRECEDING | unsigned_value PRECEDING | CURRENT ROW } |
{BETWEEN
{ UNBOUNDED PRECEDING | unsigned_value { PRECEDING | FOLLOWING } |
CURRENT ROW}
AND
{ UNBOUNDED FOLLOWING | unsigned_value { PRECEDING | FOLLOWING } |
CURRENT ROW }}
The function gets applied to an expression of columns that you’re extracting from your query. The amount of information that the function is applied to is controlled by PARTITION BY. How the information is ordered within the window is then controlled by the ORDER BY.
This is all text-booky kind of stuff, so let’s apply it to some actual data in the database.
Sample Data
For today’s article, lets consider a really simple sales breakdown. No relationships to other table; everything is locallised and contained in here (no that it needs to be - it just makes the example simpler).
This offers the same result, with a much cleaner query experience.
To look into this data a little further, and perform the same operation over the salesperson’s averages, we simply swap out the PARTITION BY expression:
This now tells us how each of the saleperson’s sales performed against their average.
Functions
With a basic understanding of the windowing function framework and how it can be applied to your queries, any of the supported functions can be use. The actual documentation of these functions can be found in the AWS documentation for Redshift, so I won’t reproduce that material here.
The list of functions are:
AVG
COUNT
CUME_DIST
DENSE_RANK
FIRST_VALUE
LAST_VALUE
LAG
LEAD
LISTAGG
MAX
MEDIAN
MIN
NTH_VALUE
NTILE
PERCENT_RANK
PERCENTILE_CONT
PERCENTILE_DISC
RANK
RATIO_TO_REPORT
ROW_NUMBER
STDDEV_SAMP
STDDEV_POP
SUM
VAR_SAMP
VAR_POP
In future articles I’ll cover these functions and how they can be used to perform analysis over your datasets.
This article is an extension of the window function, to follow up on each of the window functions.
The FIRST_VALUE and LAST_VALUE functions will allow you to retrieve the first and last values for a given window (respectively).
Taking the sales example for another run, we can compare every single sale for the day to see how it stacked up against the first sale of the day with the following query:
This demonstrates how the first value can be drawn out of a set, per window.
Inversely, you simply use the LAST_VALUE function to get the last value; if you wanted to see how each sale stacked up against the last sale of the day: