ColumnStore Window Functions

You are viewing an old version of this article. View the current version here.

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):

idaccountNamenameowneramountcloseDatestageName
1BrowseblabMulti-lateral executive functionBob26444.862016-10-20Negotiating
2MitaOrganic demand-driven benchmarkMaria477878.412016-11-28ClosedWon
3MibooDe-engineered hybrid groupwareOlivier80181.782017-01-05ClosedWon
4YoubridgeEnterprise-wide bottom-line Graphic InterfaceChris946245.292016-07-02ClosedWon
5SkybaReverse-engineered fresh-thinking standardizationMaria696241.822017-02-17Negotiating
6EayoFundamental well-modulated artificial intelligenceBob765605.522016-08-27Prospecting
7YotzExtended secondary infrastructureChris319624.202017-01-06ClosedLost
8OlooConfigurable web-enabled data-warehouseChris321016.262017-03-08ClosedLost
9KaymboMulti-lateral web-enabled definitionBob690881.012017-01-02Developing
10RhylooPublic-key coherent infrastructureChris965477.742016-11-07Prospecting

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:

quartileminmax
16337.15287634.01
2288796.14539977.45
3540070.04748727.51
4753670.77998864.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:

owneraccountNameCloseDateamountcumeWonrunningMax
BillBabbleopia2016-10-02437636.47437636.47437636.47
BillThoughtworks2016-10-04146086.51583722.98437636.47
OlivierDevpulse2016-10-05834235.931417958.91834235.93
ChrisLinkbridge2016-10-07539977.452458738.65834235.93
OlivierTrupe2016-10-07500802.291918761.20834235.93
BillLatz2016-10-08857254.873315993.52857254.87
ChrisAvamm2016-10-09699566.864015560.38857254.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;

owneraccountNameCloseDateamountcumeWonrunningMax
BillBabbleopia2016-10-02437636.47437636.47437636.47
BillThoughtworks2016-10-04146086.51583722.98437636.47
BillLatz2016-10-08857254.871440977.85857254.87
ChrisLinkbridge2016-10-07539977.45539977.45539977.45
ChrisAvamm2016-10-09699566.861239544.31699566.86
OlivierDevpulse2016-10-05834235.93834235.93834235.93
OlivierTrupe2016-10-07500802.291335038.22834235.93

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.