34
loading...
This website collects cookies to deliver better user experience
Functions | What it does |
---|---|
AVG(args) | Average (arithmetic mean) of all the non-null input values within a window frame. |
COUNT(args) | Count of non-null values within a window frame. |
MAX(args) | Maximum value of the non-null values within a window frame. |
MIN(args) | Minimum value of the non-null values within a window frame. |
SUM(args) | Sum of non-null values within a window frame. |
SELECT customer_id AS cid,
billing_city AS city,
total,
ROUND(AVG(total) OVER(PARTITION BY billing_city), 2) AS avg,
COUNT(total) OVER(PARTITION BY billing_city) AS count,
MAX(total) OVER(PARTITION BY billing_city) AS max,
MIN(total) OVER(PARTITION BY billing_city) AS min,
SUM(total) OVER(PARTITION BY billing_city) AS sum
FROM invoice;
cid | city | total | avg | count | max | min | sum
-----+-----------+-------+------+-------+-------+------+-------
48 | Amsterdam | 1.98 | 5.80 | 7 | 13.86 | 0.99 | 40.62
48 | Amsterdam | 13.86 | 5.80 | 7 | 13.86 | 0.99 | 40.62
48 | Amsterdam | 0.99 | 5.80 | 7 | 13.86 | 0.99 | 40.62
48 | Amsterdam | 8.91 | 5.80 | 7 | 13.86 | 0.99 | 40.62
48 | Amsterdam | 8.94 | 5.80 | 7 | 13.86 | 0.99 | 40.62
48 | Amsterdam | 3.96 | 5.80 | 7 | 13.86 | 0.99 | 40.62
48 | Amsterdam | 1.98 | 5.80 | 7 | 13.86 | 0.99 | 40.62
59 | Bangalore | 1.99 | 6.11 | 6 | 13.86 | 1.98 | 36.64
59 | Bangalore | 8.91 | 6.11 | 6 | 13.86 | 1.98 | 36.64
59 | Bangalore | 1.98 | 6.11 | 6 | 13.86 | 1.98 | 36.64
59 | Bangalore | 13.86 | 6.11 | 6 | 13.86 | 1.98 | 36.64
59 | Bangalore | 3.96 | 6.11 | 6 | 13.86 | 1.98 | 36.64
59 | Bangalore | 5.94 | 6.11 | 6 | 13.86 | 1.98 | 36.64
<------------------------ TRUNCATED ------------------------->
Functions | What it does |
---|---|
ROW_NUMBER() | Returns the number of the current row within a window partition. The numbers are unique. |
RANK() | Returns the rank of the current row within a partition. There will be gaps and tied values. |
DENSE_RANK() | Returns the rank of the current row within a partition, without gaps, but there will be tied values. Think of it as counts for peer groups. |
SELECT customer_id AS cid,
billing_city AS city,
total,
ROW_NUMBER() OVER(PARTITION BY billing_city) AS row_number,
RANK() OVER(PARTITION BY billing_city ORDER BY total) AS rank,
DENSE_RANK() OVER(PARTITION BY billing_city ORDER BY total) AS dense_rank
FROM invoice;
cid | city | total | row_number | rank | dense_rank
-----+--------------+-------+------------+------+------------
48 | Amsterdam | 0.99 | 1 | 1 | 1
48 | Amsterdam | 1.98 | 2 | 2 | 2
48 | Amsterdam | 1.98 | 3 | 2 | 2
48 | Amsterdam | 3.96 | 4 | 4 | 3
48 | Amsterdam | 8.91 | 5 | 5 | 4
48 | Amsterdam | 8.94 | 6 | 6 | 5
48 | Amsterdam | 13.86 | 7 | 7 | 6
59 | Bangalore | 1.98 | 1 | 1 | 1
59 | Bangalore | 1.99 | 2 | 2 | 2
59 | Bangalore | 3.96 | 3 | 3 | 3
59 | Bangalore | 5.94 | 4 | 4 | 4
59 | Bangalore | 8.91 | 5 | 5 | 5
59 | Bangalore | 13.86 | 6 | 6 | 6
<------------------------ TRUNCATED ------------------------->
SELECT customer_id AS cid,
billing_city AS city,
total,
PERCENT_RANK() over(PARTITION BY billing_city ORDER BY total)
FROM invoice;
city | total | percent_rank
--------------------+-------+---------------------
Amsterdam | 0.99 | 0
Amsterdam | 1.98 | 0.16666666666666666
Amsterdam | 1.98 | 0.16666666666666666
Amsterdam | 3.96 | 0.5
Amsterdam | 8.91 | 0.6666666666666666
Amsterdam | 8.94 | 0.8333333333333334
Amsterdam | 13.86 | 1
Bangalore | 1.98 | 0
Bangalore | 1.99 | 0.2
Bangalore | 3.96 | 0.4
Bangalore | 5.94 | 0.6
Bangalore | 8.91 | 0.8
Bangalore | 13.86 | 1
<---------------- TRUNCATED ---------------->
SELECT billing_city as city,
total,
CUME_DIST() over(PARTITION BY billing_city ORDER BY total)
FROM invoice;
city | total | cume_dist
--------------------------+-------+---------------------
Amsterdam | 0.99 | 0.14285714285714285
Amsterdam | 1.98 | 0.42857142857142855
Amsterdam | 1.98 | 0.42857142857142855
Amsterdam | 3.96 | 0.5714285714285714
Amsterdam | 8.91 | 0.7142857142857143
Amsterdam | 8.94 | 0.8571428571428571
Amsterdam | 13.86 | 1
Bangalore | 1.98 | 0.16666666666666666
Bangalore | 1.99 | 0.3333333333333333
Bangalore | 3.96 | 0.5
Bangalore | 5.94 | 0.6666666666666666
Bangalore | 8.91 | 0.8333333333333334
Bangalore | 13.86 | 1
::
operator to match the column data type.SELECT billing_city AS city,
invoice_date::DATE as date,
total,
LEAD(total) OVER(
PARTITION BY billing_city
ORDER BY invoice_date
) as lead,
LAG(total, 2, 0::numeric) OVER(
PARTITION BY billing_city
ORDER BY invoice_date
RANGE BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
) as lag
FROM invoice;
city | date | total | lead | lag
-------------------+------------+-------+-------+-------
Amsterdam | 2009-05-10 | 8.91 | 1.98 | 0
Amsterdam | 2010-12-15 | 1.98 | 3.96 | 0
Amsterdam | 2011-03-19 | 3.96 | 8.94 | 8.91
Amsterdam | 2011-06-21 | 8.94 | 0.99 | 1.98
Amsterdam | 2012-02-09 | 0.99 | 1.98 | 3.96
Amsterdam | 2013-08-02 | 1.98 | 13.86 | 8.94
Amsterdam | 2013-09-12 | 13.86 | | 0.99
Bangalore | 2009-04-05 | 3.96 | 5.94 | 0
Bangalore | 2009-07-08 | 5.94 | 1.99 | 0
Bangalore | 2010-02-26 | 1.99 | 1.98 | 3.96
Bangalore | 2011-08-20 | 1.98 | 13.86 | 5.94
Bangalore | 2011-09-30 | 13.86 | 8.91 | 1.99
Bangalore | 2012-05-30 | 8.91 | | 1.98
<------------------ TRUNCATED -------------------->
SELECT billing_city AS city,
invoice_date::DATE as date,
total,
FIRST_VALUE(total) OVER(
PARTITION BY billing_city
ORDER BY invoice_date
) as first,
LAST_VALUE(total) OVER(
PARTITION BY billing_city
ORDER BY invoice_date
RANGE BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
) as last
FROM invoice;
city | date | total | first | last
-------------------+------------+-------+-------+-------
Amsterdam | 2009-05-10 | 8.91 | 8.91 | 13.86
Amsterdam | 2010-12-15 | 1.98 | 8.91 | 13.86
Amsterdam | 2011-03-19 | 3.96 | 8.91 | 13.86
Amsterdam | 2011-06-21 | 8.94 | 8.91 | 13.86
Amsterdam | 2012-02-09 | 0.99 | 8.91 | 13.86
Amsterdam | 2013-08-02 | 1.98 | 8.91 | 13.86
Amsterdam | 2013-09-12 | 13.86 | 8.91 | 13.86
Bangalore | 2009-04-05 | 3.96 | 3.96 | 8.91
Bangalore | 2009-07-08 | 5.94 | 3.96 | 8.91
Bangalore | 2010-02-26 | 1.99 | 3.96 | 8.91
Bangalore | 2011-08-20 | 1.98 | 3.96 | 8.91
Bangalore | 2011-09-30 | 13.86 | 3.96 | 8.91
Bangalore | 2012-05-30 | 8.91 | 3.96 | 8.91
<------------------- TRUNCATED ------------------->
SELECT billing_city AS city,
total,
NTILE(3) OVER(PARTITION BY billing_city)
FROM invoice;
city | total | ntile
--------------------------+-------+-------
Amsterdam | 1.98 | 1
Amsterdam | 13.86 | 1
Amsterdam | 0.99 | 1
Amsterdam | 8.91 | 2
Amsterdam | 8.94 | 2
Amsterdam | 3.96 | 3
Amsterdam | 1.98 | 3
Bangalore | 1.99 | 1
Bangalore | 8.91 | 1
Bangalore | 1.98 | 2
Bangalore | 13.86 | 2
Bangalore | 3.96 | 3
Bangalore | 5.94 | 3
<------------- TRUNCATED ----------->
SELECT billing_city AS city,
total,
NTH_VALUE(total, 2) OVER(
PARTITION BY billing_city
ORDER BY total DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
FROM invoice;
city | total | nth_value
--------------------------+-------+-----------
Amsterdam | 13.86 | 8.94
Amsterdam | 8.94 | 8.94
Amsterdam | 8.91 | 8.94
Amsterdam | 3.96 | 8.94
Amsterdam | 1.98 | 8.94
Amsterdam | 1.98 | 8.94
Amsterdam | 0.99 | 8.94
Bangalore | 13.86 | 8.91
Bangalore | 8.91 | 8.91
Bangalore | 5.94 | 8.91
Bangalore | 3.96 | 8.91
Bangalore | 1.99 | 8.91
Bangalore | 1.98 | 8.91
<--------------- TRUNCATED ------------->
Functions | Requires ORDER BY | Accept Window Frame |
---|---|---|
avg() | No | Yes |
count() | No | Yes |
max() | No | Yes |
min() | No | Yes |
sum() | No | Yes |
row_number() | No | No |
rank() | Yes | No |
dense_rank() | Yes | No |
percent_rank() | Yes | No |
cume_dist() | Yes | No |
lead() | Yes | No |
lag() | Yes | No |
first_value() | No | Yes |
last_value() | No | Yes |
ntile() | Yes | No |
nth_value() | No | Yes |