COUNT_EACH

The number of items grouped by the given key.

Syntax

COUNT_EACH([MAX VALUES, ]GROUP)

Arguments

MAX VALUES: The maximum number of entries that can be counted (default: 2,147,483,647). When MAX VALUES is omitted, the limit is simply set to the default value. GROUP: The field you would like counted.

Returns

An integer

Notes

"EACH" Aggregate Functions, such as this one, only work when emitting hierarchical data.

Example

Data

[
   {
      "user":"A01",
      "level":1,
      "score":25
   },
   {
      "user":"A01",
      "level":2,
      "score":48
   },
   {
      "user":"A01",
      "level":2,
      "score":81
   },
   {
      "user":"A01",
      "level":2,
      "score":81
   },
   {
      "user":"B02",
      "level":1,
      "score":23
   },
   {
      "user":"B02",
      "level":1,
      "score":29
   }
]

Query

Display hierarchical results of each value, with the matching count of the value. This resembles a count field + group by query, except that the results are formatted as a single hierarchy.

SELECT
   data.User AS user:STRING,
   data.Level AS level:BIGINT,
   COUNT_EACH(data.User) AS count_each_data_user 
FROM
   "MAX_SAMPLE_DATA_2" 
GROUP BY
   data.User,
   data.Level

Results

{
   "level":2,
   "user":"A01",
   "count_each_score":[
      {
         "value":1,
         "key":48
      },
      {
         "value":2,
         "key":81
      }
   ]
}{
   "level":1,
   "user":"B02",
   "count_each_score":[
      {
         "value":1,
         "key":23
      },
      {
         "value":1,
         "key":29
      }
   ]
}{
   "level":1,
   "user":"A01",
   "count_each_score":[
      {
         "value":1,
         "key":25
      }
   ]
}

COUNT COUNT(*) COUNT DISTINCT

Dialog

Last updated