Aggregate Functions

Aggregate functions take multiple values from documents, perform calculations, and return a single value as the result. The function names are case insensitive.

You can only use aggregate functions in SELECT, LETTING, HAVING, and ORDER BY clauses. When using an aggregate function in a query, the query operates as an aggregate query.

Aggregate functions take one argument, which is used to compute the aggregate function. The COUNT function can also take a wildcard () or a path with a wildcard (path.) as its argument.

If there is no input row for the group, COUNT functions return 0. All other aggregate functions return NULL.

ARRAY_AGG(expression)

Returns array of the non-MISSING values in the group, including NULL values.

Example 1: List all values of the Cleanliness reviews given.

SELECT ARRAY_AGG(reviews[0].ratings.Cleanliness) AS Reviews FROM `travel-sample`;

Results:
[
  {
    "Reviews": [
      -1,
      -1,
      -1,
      -1,
      -1,
...

ARRAY_AGG(DISTINCT expression)

Returns array of the distinct non-MISSING values in the group, including NULL values.

Example 2: List all unique values of the Cleanliness reviews given.

SELECT ARRAY_AGG(DISTINCT reviews[0].ratings.Cleanliness) AS Reviews FROM `travel-sample`;

Results:[
  {
    "UniqueReviews": [
      -1,
      1,
      2,
      3,
      4,
      5
    ]
  }
]

AVG(expression)

Returns arithmetic mean (average) of all the number values in the group.

Example 3: The average altitude of airports in the travel-sample keyspace.

SELECT AVG(geo.alt) AS AverageAltitude FROM `travel-sample` WHERE type="airport";

Results:
[
  {
    "AverageAltitude": 870.1651422764228
  }
]

AVG(DISTINCT expression)

Returns arithmetic mean (average) of all the distinct number values in the group.

Example 4: The average number of airline route stops vs. the DISTINCT average of airline route stops.

SELECT AVG(stops) FROM `travel-sample`;              / Results in 0.0002 since nearly all docs
                                                     / have 0 stops.

SELECT AVG(DISTINCT stops) FROM `travel-sample`;     / Results in 0.5 since the docs contain only
                                                     / 1 or 0 stops.

COUNT(*)

Returns count of all the input rows for the group, regardless of value.

Example 5: The number of documents in travel-sample.

SELECT COUNT(*) AS CountAll FROM `travel-sample`;

Results:
[
  {
    "CountAll": 31591
  }
]

COUNT(expression)

Returns count of all the non-NULL and non-MISSING values in the group.

Example 6: The number of documents with an airline route stop in travel-sample regardless of its value.

SELECT COUNT(stops) AS CountOfStops FROM `travel-sample`;

Results:
[
  {
    "CountOfStops": 24024
  }
]

COUNT(DISTINCT expression)

Returns count of all the distinct non-NULL and non-MISSING values in the group.

Example 7: The number of unique values of airline route stops in travel-sample.

SELECT COUNT(DISTINCT stops) AS CountOfDistinctStops FROM `travel-sample`;

Results:
[
  {
    "CountOfSDistinctStops": 2             / because there are only 0 or 1 stops
  }
]

MAX(expression)

Returns the maximum non-NULL, non-MISSING value in the group in N1QL collation order.

Example 8a: Max of an integer field – Find the northernmost latitude of any hotel in travel-sample.

SELECT MAX(geo.lat) AS MaxLatitude FROM `travel-sample` WHERE type="hotel";

Result:
[
  {
    "MaxLatitude": 60.15356
  }
]

Example 8b: Max of a string field – Find the hotel whose name is last alphabetically in travel-sample.

SELECT MAX(name) AS MaxName FROM `travel-sample` WHERE type="hotel";

Result:
[
  {
    "MaxName": "pentahotel Birmingham"
  }
]

That result might have been surprising since lowercase letters come after uppercase letters and are therefore "higher" than uppercase letters. To avoid this uppercase/lowercase confusion, you should first make all values uppercase or lowercase, as in the following example.

Example 8c: Max of a string field, regardless of case – Find the hotel whose name is last alphabetically in travel-sample.

SELECT MAX(UPPER(name)) AS MaxName FROM `travel-sample` WHERE type="hotel";

Result:
[
  {
    "MaxName": "YOSEMITE LODGE AT THE FALLS"
  }
]

MIN(expression)

Returns the minimum non-NULL, non-MISSING value in the group in N1QL collation order.

Example 9a: Min of an integer field – Find the southernmost latitude of any hotel in travel-sample.

SELECT MIN(geo.lat) AS MinLatitude FROM `travel-sample` WHERE type="hotel";

Result:
[
  {
    "MinLatitude": 32.68092
  }
]

Example 9b: Min of a string field – Find the hotel whose name is first alphabetically in travel-sample.

SELECT MIN(name) AS MinName FROM `travel-sample` WHERE type="hotel";

Result:
[
  {
    "MinName": "'La Mirande Hotel"
  }
]

That result might have been surprising since some symbols come before letters and are therefore "lower" than letters. To avoid this symbol confusion, you can specify letters only, as in the following example.

Example 9c: Min of a string field, regardless of preceding non-letters – Find the first hotel alphabetically in travel-sample.

SELECT MIN(name) AS MinName FROM `travel-sample` WHERE type="hotel" AND SUBSTR(name,0)>="A";

Result:
[
  {
    "MinName": "AIRE NATURELLE LE GROZEAU Aire naturelle"
  }
]

SUM(expression)

Returns sum of all the number values in the group.

Example 10: The sum total of all airline route stops in travel-sample. NOTE: In the travel-sample bucket, nearly all flights are non-stop (0 stops) and only six flights have 1 stop, so we expect 6 flights of 1 stop each, a total of 6.

SELECT SUM(stops) AS SumOfStops FROM `travel-sample`;

Results:
[
  {
    "SumOfStops": 6                  / because there are 6 routes * 1 stop each = 6
  }
]

SUM(DISTINCT expression)

Returns arithmetic sum of all the distinct number values in the group.

Example 11: The sum total of all unique numbers of airline route stops in travel-sample.

SELECT SUM(DISTINCT stops) AS SumOfDistinctStops FROM `travel-sample`;

Results:
[
  {
    "SumOfDistinctStops": 1          / because there are only 0 and 1 stops per route; and 0 + 1 = 1
  }
]

GROUP BY Clause for GROUP BY, LETTING, and HAVING clauses.