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 input values

Synopsis

count() -> uint64

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::uint64

Mixed types are handled:

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

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::uint64}
{k:2,count:1::uint64}

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 where clause in the aggregation:

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

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::uint64