Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Aggregate Function

count — count all input values

Tip

For a running count as values arrive, see the count operator.

Synopsis

count() -> int64

Description

The count aggregate function computes the number of values in its input.

Examples

Count of values in a simple sequence:

# spq
count()
# input
1
2
3
# expected output
3

Mixed types are handled:

# spq
count()
# input
1
"foo"
10.0.0.1
# expected output
3

Count of values in buckets grouped by key:

# spq
count() by k | sort
# input
{a:1,k:1}
{a:2,k:1}
{a:3,k:2}
# expected output
{k:1,count:2}
{k:2,count:1}

A simple count with no input values returns no output:

# spq
where grep("bar", this) | count()
# input
1
"foo"
10.0.0.1
# expected output

Count can return an explicit zero when using a filter clause in the aggregation:

# spq
count() filter (grep("bar", this))
# input
1
"foo"
10.0.0.1
# expected output
0

Note that the number of input values are counted, unlike the len function which counts the number of elements in a given value:

# spq
count()
# input
[1,2,3]
# expected output
1