ColumnStore Window Functions
Introduction
MariaDB ColumnStore provides support for window functions broadly following the SQL 2003 specification as supported by many other databases.
Supported Functions
Examples
Example Schema
The examples are all based on the following simplified sales opportunity table:
create table opportunities ( id int, accountName varchar(20), name varchar(128), owner varchar(7), amount decimal(10,2), closeDate date, stageName varchar(11) ) engine=columnstore;
Some example values are (thanks to https://www.mockaroo.com for sample data generation):
id | accountName | name | owner | amount | closeDate | stageName |
---|---|---|---|---|---|---|
1 | Browseblab | Multi-lateral executive function | Bob | 26444.86 | 2016-10-20 | Negotiating |
2 | Mita | Organic demand-driven benchmark | Maria | 477878.41 | 2016-11-28 | ClosedWon |
3 | Miboo | De-engineered hybrid groupware | Olivier | 80181.78 | 2017-01-05 | ClosedWon |
4 | Youbridge | Enterprise-wide bottom-line Graphic Interface | Chris | 946245.29 | 2016-07-02 | ClosedWon |
5 | Skyba | Reverse-engineered fresh-thinking standardization | Maria | 696241.82 | 2017-02-17 | Negotiating |
6 | Eayo | Fundamental well-modulated artificial intelligence | Bob | 765605.52 | 2016-08-27 | Prospecting |
7 | Yotz | Extended secondary infrastructure | Chris | 319624.20 | 2017-01-06 | ClosedLost |
8 | Oloo | Configurable web-enabled data-warehouse | Chris | 321016.26 | 2017-03-08 | ClosedLost |
9 | Kaymbo | Multi-lateral web-enabled definition | Bob | 690881.01 | 2017-01-02 | Developing |
10 | Rhyloo | Public-key coherent infrastructure | Chris | 965477.74 | 2016-11-07 | Prospecting |
Quartiles Example
The NTile window function allows for breaking up a data set into portions assigned a numeric value to each portion of the range. NTile(4) breaks the data up into quartiles (4 sets).
select t.quartile, min(t.amount) min, max(t.amount) max from ( select amount, ntile(4) over (order by amount asc) quartile from opportunities where closeDate >= '2016-10-01' and closeDate <= '2016-12-31' ) t group by quartile order by quartile;
With example results:
quartile | min | max |
---|---|---|
1 | 6337.15 | 287634.01 |
2 | 288796.14 | 539977.45 |
3 | 540070.04 | 748727.51 |
4 | 753670.77 | 998864.47 |
Cumulative Sum and Running Max Example
Window functions can be used to achieve cumulative / running calculations on a detail report. In this case a won opportunity report for a 7 day period adds columns to show the accumulated won amount as well as the current highest opportunity amount in preceding rows.
select owner, accountName, CloseDate, amount, sum(amount) over (order by CloseDate rows between unbounded preceding and current row) cumeWon, max(amount) over (order by CloseDate rows between unbounded preceding and current row) runningMax from opportunities where stageName='ClosedWon' and closeDate >= '2016-10-02' and closeDate <= '2016-10-09' order by CloseDate;
with example results:
owner | accountName | CloseDate | amount | cumeWon | runningMax |
---|---|---|---|---|---|
Bill | Babbleopia | 2016-10-02 | 437636.47 | 437636.47 | 437636.47 |
Bill | Thoughtworks | 2016-10-04 | 146086.51 | 583722.98 | 437636.47 |
Olivier | Devpulse | 2016-10-05 | 834235.93 | 1417958.91 | 834235.93 |
Chris | Linkbridge | 2016-10-07 | 539977.45 | 2458738.65 | 834235.93 |
Olivier | Trupe | 2016-10-07 | 500802.29 | 1918761.20 | 834235.93 |
Bill | Latz | 2016-10-08 | 857254.87 | 3315993.52 | 857254.87 |
Chris | Avamm | 2016-10-09 | 699566.86 | 4015560.38 | 857254.87 |
Partitioned Cumulative Sum and Running Max Example
The above example can be partitioned, so that the window functions are over a particular field grouping such as owner and accumulate within that grouping.
select owner, accountName, CloseDate, amount, sum(amount) over (partition by owner order by CloseDate rows between unbounded preceding and current row) cumeWon, max(amount) over (partition by owner order by CloseDate rows between unbounded preceding and current row) runningMax from opportunities where stageName='ClosedWon' and closeDate >= '2016-10-02' and closeDate <= '2016-10-09' order by owner, CloseDate;
owner | accountName | CloseDate | amount | cumeWon | runningMax |
---|---|---|---|---|---|
Bill | Babbleopia | 2016-10-02 | 437636.47 | 437636.47 | 437636.47 |
Bill | Thoughtworks | 2016-10-04 | 146086.51 | 583722.98 | 437636.47 |
Bill | Latz | 2016-10-08 | 857254.87 | 1440977.85 | 857254.87 |
Chris | Linkbridge | 2016-10-07 | 539977.45 | 539977.45 | 539977.45 |
Chris | Avamm | 2016-10-09 | 699566.86 | 1239544.31 | 699566.86 |
Olivier | Devpulse | 2016-10-05 | 834235.93 | 834235.93 | 834235.93 |
Olivier | Trupe | 2016-10-07 | 500802.29 | 1335038.22 | 834235.93 |