Aggregate and analytic functions: SUM, AVG, COUNT, RANK, LAG, PERCENTILE_CONT and more
Aggregate functions compute one value from a group of rows: with GROUP BY there is one result row per group, without it one row for the whole result. Analytic functions (window functions) compute a value for every row from a group of related rows, the window, defined by an OVER clause; the rows themselves are not collapsed.
Most functions on this page can be used both ways:
| Function | Aggregate | Analytic | Description |
|---|---|---|---|
| AVG | ✓ | ✓ | Average |
| COUNT | ✓ | ✓ | Number of rows or values |
| CUME_DIST | ✓ | ✓ | Cumulative distribution |
| DENSE_RANK | ✓ | Rank without gaps | |
| FIRST_VALUE | ✓ | First value of the window | |
| GROUP_CONCAT | ✓ | Values joined into a string | |
| JSON_ARRAYAGG | ✓ | Values as a JSON array | |
| JSON_OBJECTAGG | ✓ | Key/value pairs as a JSON object | |
| LAG | ✓ | Value from a previous row | |
| LAST_VALUE | ✓ | Last value of the window | |
| LEAD | ✓ | Value from a following row | |
| MAX, MIN | ✓ | ✓ | Largest and smallest value |
| MEDIAN | ✓ | ✓ | Median |
| NTH_VALUE | ✓ | N-th value of the window | |
| NTILE | ✓ | Bucket number | |
| PERCENT_RANK | ✓ | ✓ | Relative rank |
| PERCENTILE_CONT, PERCENTILE_DISC | ✓ | ✓ | Percentile (interpolated or actual value) |
| RANK | ✓ | Rank with gaps after ties | |
| ROW_NUMBER | ✓ | Sequential row number | |
| STDDEV, STDDEV_POP, STDDEV_SAMP | ✓ | ✓ | Standard deviation |
| SUM | ✓ | ✓ | Sum |
| VARIANCE, VAR_POP, VAR_SAMP | ✓ | ✓ | Variance |
The list function COLLECT_LIST is also an aggregate function.
Aggregate functions
Section titled “Aggregate functions”An aggregate function returns one value per group. NULL values are ignored, except by COUNT(*). Most aggregate functions accept DISTINCT (or its synonyms DISTINCTROW and UNIQUE) to use each different value only once; ALL, the default, uses every value.
The brands Diadora and Rab manufacture 9 and 11 products. Some statistics per brand:
SELECT b.name AS brand, COUNT(*) AS products, AVG(p.unit_price) AS avg_price, MIN(p.unit_price) AS min_price, MAX(p.unit_price) AS max_price, SUM(p.units_in_stock) AS stock, MEDIAN(p.unit_price) AS median_priceFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name IN ('Diadora', 'Rab')GROUP BY b.nameORDER BY b.name;| brand | products | avg_price | min_price | max_price | stock | median_price |
|---|---|---|---|---|---|---|
| Diadora | 9 | 50.333333333333336 | 28.00 | 69.00 | 282 | 47 |
| Rab | 11 | 47.18181818181818 | 16.00 | 70.00 | 511 | 42 |
To filter groups, use HAVING.
Analytic functions
Section titled “Analytic functions”function_name ( [argument_list] ) OVER ( [PARTITION BY expression [, expression] ...] [ORDER BY expression [ASC | DESC] [, ...]] )- PARTITION BY divides the rows into groups (partitions); the function is computed separately in each. Without it, all rows of the result form one partition.
- ORDER BY sorts the rows inside each partition. Ranking and offset functions use this order. For aggregate functions used with
OVER, an ORDER BY makes the result cumulative: each row's value covers the rows from the start of the partition up to the current row and its ties. Without ORDER BY, the value covers the whole partition. - A constant such as
1in PARTITION BY or ORDER BY refers to a column of the select list by position; a constant expression such as1+0is ignored.
Explicit window frames (ROWS BETWEEN ..., RANGE BETWEEN ...) are not supported.
Every product of the two brands next to the average, the highest price and the number of products of its brand:
SELECT b.name AS brand, p.name AS product, p.unit_price AS price, AVG(p.unit_price) OVER (PARTITION BY b.name) AS brand_avg, MAX(p.unit_price) OVER (PARTITION BY b.name) AS brand_max, COUNT(*) OVER (PARTITION BY b.name) AS brand_productsFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name IN ('Diadora', 'Rab')ORDER BY b.name, p.unit_price DESC, p.name;| brand | product | price | brand_avg | brand_max | brand_products |
|---|---|---|---|---|---|
| Diadora | Abominable Hoodie | 69.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Neve Studio Dance Jacket | 69.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Circe Hooded Ice Fleece | 68.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Helena Hooded Fleece | 55.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Kenobi Trail Jacket | 47.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Viktor LumaTech Pant | 46.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Beaumont Summit Kit | 42.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Gobi HeatTec Tee | 29.00 | 50.333333333333336 | 69.00 | 9 |
| Diadora | Bess Yoga Short | 28.00 | 50.333333333333336 | 69.00 | 9 |
| Rab | Teton Pullover Hoodie | 70.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Ajax Full-Zip Sweatshirt | 69.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Orestes Yoga Pant | 66.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Riona Full Zip Jacket | 60.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Proteus Fitness Jackshirt | 45.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Beaumont Summit Kit | 42.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Oslo Trek Hoodie | 42.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Stark Fundamental Hoodie | 42.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Strike Endurance Tee | 39.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Gabrielle Micro Sleeve Top | 28.00 | 47.18181818181818 | 70.00 | 11 |
| Rab | Pursuit Lumaflex Tone Band | 16.00 | 47.18181818181818 | 70.00 | 11 |
An analytic function can be applied to the result of a grouped query in a derived table. The orders of 2025 per month, with the previous and next month, a running total and the rank of each month:
SELECT mon, orders, LAG(orders) OVER (ORDER BY mon) AS prev_month, orders - LAG(orders) OVER (ORDER BY mon) AS diff, LEAD(orders, 1, 0) OVER (ORDER BY mon) AS next_month, SUM(orders) OVER (ORDER BY mon) AS running_total, RANK() OVER (ORDER BY orders DESC) AS rnkFROM (SELECT MONTH(order_date) AS mon, COUNT(*) AS orders FROM orders WHERE YEAR(order_date) = 2025 GROUP BY MONTH(order_date)) tORDER BY mon;| mon | orders | prev_month | diff | next_month | running_total | rnk |
|---|---|---|---|---|---|---|
| 1 | 51 | NULL | NULL | 50 | 51 | 9 |
| 2 | 50 | 51 | -1 | 46 | 101 | 10 |
| 3 | 46 | 50 | -4 | 54 | 147 | 12 |
| 4 | 54 | 46 | 8 | 56 | 201 | 8 |
| 5 | 56 | 54 | 2 | 62 | 257 | 7 |
| 6 | 62 | 56 | 6 | 50 | 319 | 6 |
| 7 | 50 | 62 | -12 | 69 | 369 | 10 |
| 8 | 69 | 50 | 19 | 67 | 438 | 4 |
| 9 | 67 | 69 | -2 | 76 | 505 | 5 |
| 10 | 76 | 67 | 9 | 110 | 581 | 3 |
| 11 | 110 | 76 | 34 | 95 | 691 | 1 |
| 12 | 95 | 110 | -15 | 0 | 786 | 2 |
With an empty OVER (), the window is the whole result. The share of each year in all orders:
SELECT yr, orders, shipping, SUM(orders) OVER () AS all_orders, ROUND(100.0 * orders / SUM(orders) OVER (), 1) AS pctFROM (SELECT YEAR(order_date) AS yr, COUNT(*) AS orders, SUM(shipping_cost) AS shipping FROM orders GROUP BY YEAR(order_date)) tORDER BY yr;| yr | orders | shipping | all_orders | pct |
|---|---|---|---|---|
| 2021 | 416 | 6754.91 | 2910 | 14.3 |
| 2022 | 482 | 7880.73 | 2910 | 16.6 |
| 2023 | 551 | 9825.75 | 2910 | 18.9 |
| 2024 | 675 | 11440.27 | 2910 | 23.2 |
| 2025 | 786 | 13346.04 | 2910 | 27 |
CUME_DIST, DENSE_RANK, LAG, LEAD, NTILE, PERCENT_RANK, RANK and ROW_NUMBER need an ORDER BY in the OVER clause; without it, an error is raised or the result has no defined order.
Most examples below use the nine Diadora products; two of them share the highest price.
AVG ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )AVG ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression ) OVER ( analytic_clause )Returns the average of the non-NULL values of a numeric expression. Return type: DOUBLE
See the examples above.
COUNT ( * )COUNT ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )COUNT ( * ) OVER ( analytic_clause )COUNT ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression ) OVER ( analytic_clause )COUNT(*) counts rows; COUNT(expression) counts the rows where expression is not NULL; COUNT(DISTINCT expression) counts the different non-NULL values. Return type: INTEGER
SELECT COUNT(*) AS orders, COUNT(shipped_date) AS shipped, COUNT(DISTINCT customer_id) AS customers FROM orders;| orders | shipped | customers |
|---|---|---|
| 2910 | 2901 | 650 |
Nine orders have not been shipped yet: their shipped_date is NULL.
CUME_DIST
Section titled “CUME_DIST”CUME_DIST ( expression [, expression] ... ) WITHIN GROUP ( ORDER BY ... )CUME_DIST () OVER ( [PARTITION BY ...] ORDER BY ... )The cumulative distribution: the fraction of rows that come before or together with the current row in the given order. The result is greater than 0 and at most 1. Return type: DOUBLE
As an aggregate function, it returns the cumulative distribution that a hypothetical row with the given values would have in the group. As an analytic function, it returns the value for each row. See PERCENT_RANK for the difference between the two.
SELECT p.name AS product, p.unit_price AS price, CUME_DIST() OVER (ORDER BY p.unit_price) AS cume, PERCENT_RANK() OVER (ORDER BY p.unit_price) AS pct_rank, NTILE(3) OVER (ORDER BY p.unit_price DESC) AS tierFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora'ORDER BY p.unit_price DESC, p.name;| product | price | cume | pct_rank | tier |
|---|---|---|---|---|
| Abominable Hoodie | 69.00 | 1 | 0.875 | 1 |
| Neve Studio Dance Jacket | 69.00 | 1 | 0.875 | 1 |
| Circe Hooded Ice Fleece | 68.00 | 0.7777777777777778 | 0.75 | 1 |
| Helena Hooded Fleece | 55.00 | 0.6666666666666666 | 0.625 | 2 |
| Kenobi Trail Jacket | 47.00 | 0.5555555555555556 | 0.5 | 2 |
| Viktor LumaTech Pant | 46.00 | 0.4444444444444444 | 0.375 | 2 |
| Beaumont Summit Kit | 42.00 | 0.3333333333333333 | 0.25 | 3 |
| Gobi HeatTec Tee | 29.00 | 0.2222222222222222 | 0.125 | 3 |
| Bess Yoga Short | 28.00 | 0.1111111111111111 | 0 | 3 |
Where would a product priced at 50 fall among the Diadora products?
SELECT CUME_DIST(50) WITHIN GROUP (ORDER BY p.unit_price) AS cume_50, PERCENT_RANK(50) WITHIN GROUP (ORDER BY p.unit_price) AS pct_rank_50FROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';| cume_50 | pct_rank_50 |
|---|---|
| 0.6 | 0.5555555555555556 |
With the hypothetical row there are 10 rows, and 6 of them are priced at 50 or less: 6/10 = 0.6. Five rows are cheaper: 5/9.
DENSE_RANK
Section titled “DENSE_RANK”DENSE_RANK () OVER ( [PARTITION BY ...] ORDER BY ... )The rank of the row in its partition. Rows with equal values get the same rank, and the next rank follows without a gap (1, 1, 2, ...). Return type: INTEGER
SELECT p.name AS product, p.unit_price AS price, RANK() OVER (ORDER BY p.unit_price DESC) AS rnk, DENSE_RANK() OVER (ORDER BY p.unit_price DESC) AS drnk, ROW_NUMBER() OVER (ORDER BY p.unit_price DESC, p.name) AS rnFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora'ORDER BY p.unit_price DESC, p.name;| product | price | rnk | drnk | rn |
|---|---|---|---|---|
| Abominable Hoodie | 69.00 | 1 | 1 | 1 |
| Neve Studio Dance Jacket | 69.00 | 1 | 1 | 2 |
| Circe Hooded Ice Fleece | 68.00 | 3 | 2 | 3 |
| Helena Hooded Fleece | 55.00 | 4 | 3 | 4 |
| Kenobi Trail Jacket | 47.00 | 5 | 4 | 5 |
| Viktor LumaTech Pant | 46.00 | 6 | 5 | 6 |
| Beaumont Summit Kit | 42.00 | 7 | 6 | 7 |
| Gobi HeatTec Tee | 29.00 | 8 | 7 | 8 |
| Bess Yoga Short | 28.00 | 9 | 8 | 9 |
FIRST_VALUE
Section titled “FIRST_VALUE”FIRST_VALUE ( expression ) [ {RESPECT | IGNORE} NULLS ] OVER ( analytic_clause )The value of expression in the first row of the window. With IGNORE NULLS, the first non-NULL value. The default is RESPECT NULLS. Return type: the type of expression
SELECT p.name AS product, p.unit_price AS price, FIRST_VALUE(p.name) OVER (ORDER BY p.unit_price DESC, p.name) AS most_expensive, LAST_VALUE(p.name) OVER (ORDER BY p.unit_price DESC, p.name) AS last_so_far, NTH_VALUE(p.name, 2) OVER (ORDER BY p.unit_price DESC, p.name) AS second_oneFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora'ORDER BY p.unit_price DESC, p.name;| product | price | most_expensive | last_so_far | second_one |
|---|---|---|---|---|
| Abominable Hoodie | 69.00 | Abominable Hoodie | Abominable Hoodie | NULL |
| Neve Studio Dance Jacket | 69.00 | Abominable Hoodie | Neve Studio Dance Jacket | Neve Studio Dance Jacket |
| Circe Hooded Ice Fleece | 68.00 | Abominable Hoodie | Circe Hooded Ice Fleece | Neve Studio Dance Jacket |
| Helena Hooded Fleece | 55.00 | Abominable Hoodie | Helena Hooded Fleece | Neve Studio Dance Jacket |
| Kenobi Trail Jacket | 47.00 | Abominable Hoodie | Kenobi Trail Jacket | Neve Studio Dance Jacket |
| Viktor LumaTech Pant | 46.00 | Abominable Hoodie | Viktor LumaTech Pant | Neve Studio Dance Jacket |
| Beaumont Summit Kit | 42.00 | Abominable Hoodie | Beaumont Summit Kit | Neve Studio Dance Jacket |
| Gobi HeatTec Tee | 29.00 | Abominable Hoodie | Gobi HeatTec Tee | Neve Studio Dance Jacket |
| Bess Yoga Short | 28.00 | Abominable Hoodie | Bess Yoga Short | Neve Studio Dance Jacket |
Because of the ORDER BY, the window of each row ends at the current row: LAST_VALUE returns the current row's value, and NTH_VALUE(..., 2) is NULL in the first row, whose window has only one row.
IGNORE NULLS skips NULL values. Order 455 has not been shipped; LAST_VALUE ... IGNORE NULLS carries the last known shipping date forward:
SELECT id, shipped_date, FIRST_VALUE(shipped_date) OVER (ORDER BY id DESC) AS fv, LAST_VALUE(shipped_date) IGNORE NULLS OVER (ORDER BY id) AS last_knownFROM orders WHERE id BETWEEN 453 AND 457 ORDER BY id;| id | shipped_date | fv | last_known |
|---|---|---|---|
| 453 | 2022-08-24 | 2023-11-04 | 2022-08-24 |
| 454 | 2023-01-29 | 2023-11-04 | 2023-01-29 |
| 455 | NULL | 2023-11-04 | 2023-01-29 |
| 456 | 2025-01-04 | 2023-11-04 | 2025-01-04 |
| 457 | 2023-11-04 | 2023-11-04 | 2023-11-04 |
GROUP_CONCAT
Section titled “GROUP_CONCAT”GROUP_CONCAT ( [DISTINCT] expression [ORDER BY {column | position} [ASC | DESC]] [SEPARATOR string] )An aggregate function that joins the non-NULL values of the group into one string, separated by string (a comma by default). It returns NULL if there are no non-NULL values. Return type: STRING
The result is limited to group_concat_max_len bytes (1,024 by default) and must also fit in string_max_size_bytes.
SELECT b.name AS brand, GROUP_CONCAT(p.name ORDER BY p.name SEPARATOR ', ') AS productsFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name IN ('Diadora', 'Rab')GROUP BY b.nameORDER BY b.name;| brand | products |
|---|---|
| Diadora | Abominable Hoodie, Beaumont Summit Kit, Bess Yoga Short, Circe Hooded Ice Fleece, Gobi HeatTec Tee, Helena Hooded Fleece, Kenobi Trail Jacket, Neve Studio Dance Jacket, Viktor LumaTech Pant |
| Rab | Ajax Full-Zip Sweatshirt, Beaumont Summit Kit, Gabrielle Micro Sleeve Top, Orestes Yoga Pant, Oslo Trek Hoodie, Proteus Fitness Jackshirt, Pursuit Lumaflex Tone Band, Riona Full Zip Jacket, Stark Fundamental Hoodie, Strike Endurance Tee, Teton Pullover Hoodie |
DISTINCT removes duplicates; here the two products priced at 69.00 give one value:
SELECT GROUP_CONCAT(DISTINCT p.unit_price ORDER BY p.unit_price DESC) AS pricesFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';| prices |
|---|
| 69.00,68.00,55.00,47.00,46.00,42.00,29.00,28.00 |
JSON_ARRAYAGG
Section titled “JSON_ARRAYAGG”JSON_ARRAYAGG ( expression )An aggregate function that returns the values of the group as a JSON array. NUMERIC values become JSON doubles. Return type: JSON
JSON_OBJECTAGG
Section titled “JSON_OBJECTAGG”JSON_OBJECTAGG ( key, value )An aggregate function that returns a JSON object with one member per row, using key as the member name and value as its value. Return type: JSON
SELECT JSON_ARRAYAGG(p.unit_price) AS prices, JSON_OBJECTAGG(p.name, p.units_in_stock) AS stockFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora' AND p.unit_price > 60;| prices | stock |
|---|---|
| [69.0,68.0,69.0] | {"Abominable Hoodie":29,"Circe Hooded Ice Fleece":113,"Neve Studio Dance Jacket":22} |
The order of the array elements is not defined. See also the JSON functions.
LAG ( expression [, offset [, default]] ) OVER ( [PARTITION BY ...] ORDER BY ... )The value of expression in the row offset rows before the current row (1 by default). If there is no such row, default is returned (NULL by default). Return type: the type of expression
See the monthly orders example.
LAST_VALUE
Section titled “LAST_VALUE”LAST_VALUE ( expression ) [ {RESPECT | IGNORE} NULLS ] OVER ( analytic_clause )The value of expression in the last row of the window. With an ORDER BY, the window ends at the current row and its ties, so the result is usually the current row's value; without ORDER BY, it is the last row of the partition. With IGNORE NULLS, the last non-NULL value. Return type: the type of expression
See the examples under FIRST_VALUE.
LEAD ( expression [, offset [, default]] ) OVER ( [PARTITION BY ...] ORDER BY ... )The value of expression in the row offset rows after the current row (1 by default). If there is no such row, default is returned (NULL by default). Return type: the type of expression
See the monthly orders example: LEAD(orders, 1, 0) returns 0 for December.
MAX, MIN
Section titled “MAX, MIN”MAX ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )MIN ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )MAX ( ... ) OVER ( analytic_clause )MIN ( ... ) OVER ( analytic_clause )The largest and smallest non-NULL value of expression: a number, a string or a date/time value. Return type: the type of expression
See the examples above.
MEDIAN
Section titled “MEDIAN”MEDIAN ( expression )MEDIAN ( expression ) OVER ( [PARTITION BY ...] )The median of the non-NULL values: the middle value, or the average of the two middle values for an even number of values. It is the same as PERCENTILE_CONT(0.5). expression must be a number or a date/time value. Return type: DOUBLE, or a date/time type for date/time values
Products of the two brands priced at 60 or more, with the median price and the 50th percentile within each brand:
SELECT b.name AS brand, p.name AS product, p.unit_price AS price, MEDIAN(p.unit_price) OVER (PARTITION BY b.name) AS brand_median, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY p.unit_price) OVER (PARTITION BY b.name) AS p50_disc, RANK() OVER (PARTITION BY b.name ORDER BY p.unit_price DESC) AS rnkFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name IN ('Diadora', 'Rab') AND p.unit_price >= 60ORDER BY b.name, p.unit_price DESC, p.name;| brand | product | price | brand_median | p50_disc | rnk |
|---|---|---|---|---|---|
| Diadora | Abominable Hoodie | 69.00 | 69 | 69 | 1 |
| Diadora | Neve Studio Dance Jacket | 69.00 | 69 | 69 | 1 |
| Diadora | Circe Hooded Ice Fleece | 68.00 | 69 | 69 | 3 |
| Rab | Teton Pullover Hoodie | 70.00 | 67.5 | 66 | 1 |
| Rab | Ajax Full-Zip Sweatshirt | 69.00 | 67.5 | 66 | 2 |
| Rab | Orestes Yoga Pant | 66.00 | 67.5 | 66 | 3 |
| Rab | Riona Full Zip Jacket | 60.00 | 67.5 | 66 | 4 |
For Rab, the median of 60, 66, 69 and 70 is the average of 66 and 69, 67.5; PERCENTILE_DISC returns the actual value 66.
NTH_VALUE
Section titled “NTH_VALUE”NTH_VALUE ( expression, N ) [ {RESPECT | IGNORE} NULLS ] OVER ( analytic_clause )The value of expression in the N-th row of the window, or NULL if the window has fewer than N rows. Return type: the type of expression
See the examples under FIRST_VALUE.
NTILE ( number_of_buckets ) OVER ( [PARTITION BY ...] ORDER BY ... )Divides the ordered rows of each partition into number_of_buckets groups of (nearly) equal size and returns the group number, from 1. When the rows do not divide evenly, the first groups get one row more. Return type: INTEGER
NTILE makes groups with the same number of rows; WIDTH_BUCKET makes groups that cover equal ranges of values.
See the tier column in the CUME_DIST example: the nine products fall into three tiers of three.
PERCENT_RANK
Section titled “PERCENT_RANK”PERCENT_RANK ( expression [, expression] ... ) WITHIN GROUP ( ORDER BY ... )PERCENT_RANK () OVER ( [PARTITION BY ...] ORDER BY ... )The relative rank of a row: (RANK - 1) / (rows in the partition - 1), from 0 to 1. As an aggregate function, the relative rank that a hypothetical row with the given values would have. Return type: DOUBLE
CUME_DIST counts the rows up to and including the current row, so its smallest value is above 0 and its largest is 1. PERCENT_RANK counts the rows before the current row, so the first row gets 0. See the CUME_DIST examples.
PERCENTILE_CONT
Section titled “PERCENTILE_CONT”PERCENTILE_CONT ( percentile ) WITHIN GROUP ( ORDER BY expression [ASC | DESC] ) [ OVER ( PARTITION BY ... ) ]The value at percentile (from 0 to 1) of the ordered values, assuming a continuous distribution: if the position falls between two values, the result is interpolated between them. NULL values are ignored. Return type: DOUBLE
PERCENTILE_DISC
Section titled “PERCENTILE_DISC”PERCENTILE_DISC ( percentile ) WITHIN GROUP ( ORDER BY expression [ASC | DESC] ) [ OVER ( PARTITION BY ... ) ]The first value whose cumulative distribution is at least percentile: always one of the actual values. Return type: the type of expression
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY p.unit_price) AS p50_cont, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY p.unit_price) AS p50_disc, PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY p.unit_price) AS p25_cont, PERCENTILE_DISC(0.25) WITHIN GROUP (ORDER BY p.unit_price) AS p25_discFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';| p50_cont | p50_disc | p25_cont | p25_disc |
|---|---|---|---|
| 47 | 47 | 42 | 42 |
With nine values, the 50th and 25th percentiles fall exactly on the 5th and 3rd values, so both functions agree; the MEDIAN example shows a case where they differ.
RANK () OVER ( [PARTITION BY ...] ORDER BY ... )The rank of the row in its partition. Rows with equal values get the same rank, and the following ranks are skipped (1, 1, 3, ...). Return type: INTEGER
See the example under DENSE_RANK.
ROW_NUMBER
Section titled “ROW_NUMBER”ROW_NUMBER () OVER ( [PARTITION BY ...] ORDER BY ... )A sequential number for each row in its partition, starting at 1. Rows with equal sort values get different numbers; add columns to the ORDER BY to make the numbering deterministic. Return type: INTEGER
See the example under DENSE_RANK.
STDDEV, STDDEV_POP
Section titled “STDDEV, STDDEV_POP”STDDEV ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )STDDEV_POP ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )STDDEV ( ... ) OVER ( analytic_clause )STDDEV_POP ( ... ) OVER ( analytic_clause )The population standard deviation: the square root of VAR_POP. STDDEV and STDDEV_POP are synonyms. Return type: DOUBLE
STDDEV_SAMP
Section titled “STDDEV_SAMP”STDDEV_SAMP ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )STDDEV_SAMP ( ... ) OVER ( analytic_clause )The sample standard deviation: the square root of VAR_SAMP. Return type: DOUBLE
SELECT STDDEV(p.unit_price) AS sd, STDDEV_POP(p.unit_price) AS sd_pop, STDDEV_SAMP(p.unit_price) AS sd_samp, VARIANCE(p.unit_price) AS var, VAR_POP(p.unit_price) AS var_pop, VAR_SAMP(p.unit_price) AS var_sampFROM MATCH (b:brand)-[m:manufactures]->(p:product)WHERE b.name = 'Diadora';| sd | sd_pop | sd_samp | var | var_pop | var_samp |
|---|---|---|---|---|---|
| 15.216949614017759 | 15.216949614017759 | 16.140012391568973 | 231.5555555555552 | 231.5555555555552 | 260.5 |
SUM ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )SUM ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression ) OVER ( analytic_clause )The sum of the non-NULL values of a numeric expression. Return type: the type of expression
See the stock column in the aggregate example, and the running total and OVER () examples under Analytic functions.
VARIANCE, VAR_POP
Section titled “VARIANCE, VAR_POP”VARIANCE ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )VAR_POP ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )VARIANCE ( ... ) OVER ( analytic_clause )VAR_POP ( ... ) OVER ( analytic_clause )The population variance: the average of the squared differences from the mean, SUM((x - AVG(x))²) / COUNT(x). VARIANCE and VAR_POP are synonyms. Return type: DOUBLE
VAR_SAMP
Section titled “VAR_SAMP”VAR_SAMP ( [DISTINCT | DISTINCTROW | UNIQUE | ALL] expression )VAR_SAMP ( ... ) OVER ( analytic_clause )The sample variance: SUM((x - AVG(x))²) / (COUNT(x) - 1). Return type: DOUBLE
See the example under STDDEV_SAMP.