View a markdown version of this page

Frequency and histogram functions - Amazon Athena

Frequency and histogram functions

Use these functions to count how often values occur in a group and to find the most frequent values. The histogram and histogram_exact functions return their counts as a MAP, with one entry for each distinct value or for each bin that you list. To compute bins of equal width, use the equi_width_bins function.

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

approx_top_k function

Returns a list of up to k values of val that occur most often in the input, from the most frequent to the least frequent. The result is an estimate: on large inputs with many distinct values, the list can differ from the exact k most frequent values.

NULL values are not counted. If there are fewer than k distinct non-NULL values, the list is shorter than k. If every value is NULL, or if there are no input rows, the result is NULL. The list holds only the values, not how often each one occurs. To get exact counts, use count(*) with GROUP BY.

Syntax

approx_top_k(val, k)

Arguments

The approx_top_k function takes the following arguments:

val

The expression whose values the function ranks by frequency. It can be of any type.

k

The number of values to return, as a constant BIGINT. It must be greater than 0 and less than 1000000.

Return type

A list of the type of val, such as VARCHAR[] for VARCHAR input or INTEGER[] for INTEGER input.

Examples

The following SQL query returns the two most frequent values. c occurs three times and b twice.

SELECT approx_top_k(x, 2) AS result FROM (VALUES ('a'), ('b'), ('b'), ('c'), ('c'), ('c') ) AS v(x)

The query returns the following result.

result

[c, b]

The following SQL query asks for five values. NULL occurs most often but is not counted, and there are only two other distinct values, so the list has two elements.

SELECT approx_top_k(x, 5) AS result FROM (VALUES ('a'), (NULL), (NULL), (NULL), ('b'), ('b') ) AS v(x)

The query returns the following result.

result

[b, a]

Considerations

k must be a constant. A k of 0 or less, or of 1000000 or more, makes the query fail.

To get only the most frequent value, take the first element of the list, as in approx_top_k(val, 1)[1].

Differences from Athena engine version 3

Athena engine version 3 has approx_most_frequent(buckets, value, capacity), which returns a map from each frequent value to its estimated count. DuckDB has no approx_most_frequent function. Use approx_top_k(value, buckets) instead, which returns only the values, as a list ordered by frequency.

histogram function

Returns a MAP of counts. With only expression, the map has one entry for each distinct value of expression that is not NULL. Each key is one of those values, and its map value is the number of times that the value occurs. The keys are in ascending order.

With bins, each bin is an upper bound. A value is counted in the smallest bin that is greater than or equal to it. Every bin appears in the result, with a count of 0 when no value falls in it. Values greater than the largest bin are counted in an extra entry, whose key is a special value of the key type, such as 2147483647 for INTEGER. That entry appears only when at least one value falls in it. To test whether a key is that entry, use the is_histogram_other_bin function.

NULL values are not counted. For empty input, or when every value of expression is NULL, the result is NULL, even when you give bins.

Syntax

histogram(expression [, bins ])

Arguments

The histogram function takes the following arguments:

expression

The values to count, of any type.

bins

The upper bounds of the bins, as a list with one bound for each bin, of the same type as expression. The list does not need to be sorted. This argument is optional.

Return type

MAP whose keys have the type of expression and whose values are UBIGINT counts, for example MAP(VARCHAR, UBIGINT).

Examples

The following SQL query counts how often each region occurs.

SELECT histogram(region) AS result FROM (VALUES ('east'), ('west'), ('east'), ('north') ) AS v(region)

The query returns the following result.

result

{east=2, north=1, west=1}

The following SQL query reads the count for one key from the map.

SELECT histogram(region)['east'] AS east_count FROM (VALUES ('east'), ('west'), ('east'), ('north') ) AS v(region)

The query returns the following result.

east_count

2

The following SQL query counts values in three bins with the upper bounds 10, 20, and 30. The value 40 is greater than every bound, so it is counted in the extra entry with the key 2147483647.

SELECT histogram(amount, [10, 20, 30]) AS result FROM (VALUES (5), (12), (18), (25), (40)) AS v(amount)

The query returns the following result.

result

{10=1, 20=2, 30=1, 2147483647=1}

Considerations

To read the count for one key, use histogram(expression)[key]. element_at(histogram(expression), key) returns a list that holds the count, not the count itself.

Differences from Athena engine version 3

In Athena engine version 3, element_at(histogram(x), key) returns the count for key. In DuckDB, element_at on a map returns a list that holds the count. To get the count itself, use histogram(x)[key].

histogram_exact function

Returns a MAP with one entry for each value in bins. The map value is the number of rows where expression equals that bin exactly, or 0 when no row does. Values that equal no bin are counted in an extra entry, whose key is a special value of the key type, such as 2147483647 for INTEGER. That entry appears only when at least one value equals no bin. To test whether a key is that entry, use the is_histogram_other_bin function.

NULL values are not counted. For empty input, or when every value of expression is NULL, the result is NULL. To treat each bin as an upper bound instead of an exact value, use the histogram function with bins.

Syntax

histogram_exact(expression, bins)

Arguments

The histogram_exact function takes the following arguments:

expression

The values to count, of any type.

bins

The bins, as a list of the values to count, of the same type as expression.

Return type

MAP whose keys have the type of expression and whose values are UBIGINT counts, for example MAP(INTEGER, UBIGINT).

Examples

The following SQL query counts the values that equal 10, 20, or 30. The values 5 and 40 equal no bin, so they are counted in the extra entry with the key 2147483647.

SELECT histogram_exact(x, [10, 20, 30]) AS result FROM (VALUES (5), (10), (10), (20), (40)) AS v(x)

The query returns the following result.

result

{10=2, 20=1, 30=0, 2147483647=2}

The following SQL query has no value outside the bins, so the result has no extra entry. The NULL value is not counted.

SELECT histogram_exact(x, [1, 2, 3]) AS result FROM (VALUES (1), (2), (2), (NULL)) AS v(x)

The query returns the following result.

result

{1=1, 2=2, 3=0}

is_histogram_other_bin function

Returns true if value is the placeholder key of the extra bin in which histogram and histogram_exact count values outside the bins that you list, and false otherwise. If value is NULL, the result is NULL. Use the function to label or filter the extra bin when you expand a histogram into rows.

When you pass a list of bin boundaries to histogram, or a list of values to histogram_exact, the result has one extra bin. That bin counts every value that falls outside the listed bins. The key of that bin is a placeholder that depends on the key type. It is the largest value for integer types, such as 2147483647 for INTEGER, infinity for DOUBLE, and an empty string for VARCHAR.

Syntax

is_histogram_other_bin(value)

Arguments

The is_histogram_other_bin function takes the following argument:

value

The value to test, of any type. It is usually a key of the map that histogram or histogram_exact returns.

Return type

BOOLEAN

Examples

The following SQL query counts three values in the bins up to 10 and up to 20. The value 25 is greater than every boundary, so it is counted in the extra bin, whose key is the largest INTEGER.

SELECT e.key AS bin, e.value AS row_count, is_histogram_other_bin(e.key) AS is_other_bin FROM ( SELECT unnest(map_entries(histogram(x, [10, 20]))) AS e FROM (VALUES (5), (15), (25)) AS v(x) ) AS t ORDER BY e.key

The query returns the following results.

bin row_count is_other_bin

10

1

false

20

1

false

2147483647

1

true

The following SQL query tests three values. An empty string and the largest INTEGER are placeholders, and 20 is not.

SELECT is_histogram_other_bin('') AS empty_string, is_histogram_other_bin(2147483647) AS largest_integer, is_histogram_other_bin(20) AS regular_value

The query returns the following result.

empty_string largest_integer regular_value

true

true

false

Considerations

The function checks only the value, not where it came from. A real bin key or data value that equals the placeholder, such as an empty string or the largest INTEGER, also returns true.

mode function

Returns the value of x that occurs most often in the group. NULL values are not counted, even when NULL occurs more often than any other value. If the group has no values that are not NULL, the result is NULL.

When two or more values tie for the highest count, the result depends on the order in which the function reads the rows, which is not guaranteed. To get a predictable result, add ORDER BY inside the call. The tie then goes to the value that comes first in that order.

Syntax

mode(x [ ORDER BY sort_expression [ ASC | DESC ] [, ...] ])

Arguments

The mode function takes the following arguments:

x

The values, of any type.

sort_expression

An expression that sets the order in which the function reads the rows. When values tie, the one that comes first in this order is returned. This argument is optional.

Return type

The type of x.

Examples

The following SQL query returns the most frequent color.

SELECT mode(color) AS result FROM (VALUES ('red'), ('blue'), ('red'), ('green') ) AS v(color)

The query returns the following result.

result

red

The following SQL query has three NULL values. They are ignored, so the result is red, which occurs twice.

SELECT mode(color) AS result FROM (VALUES ('red'), (NULL), (NULL), (NULL), ('blue'), ('red') ) AS v(color)

The query returns the following result.

result

red

The following SQL query has a tie: 1 and 3 both occur twice. ORDER BY inside the call decides which value is returned.

SELECT mode(x ORDER BY x) AS lowest_tied, mode(x ORDER BY x DESC) AS highest_tied FROM (VALUES (3), (1), (1), (3)) AS v(x)

The query returns the following result.

lowest_tied highest_tied

1

3

The following SQL query returns the region that has the most orders in a table.

SELECT mode(region) AS busiest_region FROM sales_db.sales_parquet

The query returns the following result.

busiest_region

east