Skip to main content
Version: Java (Groovy)

countBy

countBy returns the number of rows for each group.

Syntax

table.countBy(countColumnName)
table.countBy(countColumnName, groupByColumns...)

Parameters

ParameterTypeDescription
countColumnNameString

The name of the output column containing the count.

groupByColumnsString...

The column(s) by which to group data.

  • NULL returns the total count of rows in the table.
  • "X" will output the count of each group in column X.
  • "X", "Y" will output the count of each group designated from the X and Y columns.
groupByColumnsColumnName...

The column(s) by which to group data.

  • NULL returns the total count of rows in the table.
  • "X" will output the count of each group in column X.
  • "X", "Y" will output the count of each group designated from the X and Y columns.
groupByColumnsCollection<String>

The column(s) by which to group data.

  • NULL returns the total count of rows in the table.
  • "X" will output the count of each group in column X.
  • "X", "Y" will output the count of each group designated from the X and Y columns.

Returns

A new table containing the number of rows for each group.

Examples

In this example, countBy returns the number of rows in the table and stores that in a new column, Count.

source = newTable(
stringCol("X", "A", "B", "A", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "N", "O", "N", "P", "M", "O", "P", "M"),
intCol("Number", 55, 76, 20, 130, 230, 50, 73, 137, 214),
)

result = source.countBy("Count")

In this example, countBy returns the number of rows in the table as grouped by X and stores that in a new column, Count.

source = newTable(
stringCol("X", "A", "B", "A", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "N", "O", "N", "P", "M", "O", "P", "M"),
intCol("Number", 55, 76, 20, 130, 230, 50, 73, 137, 214),
)

result = source.countBy("Count", "X")

In this example, countBy returns the number of rows in the table as grouped by X and Y, and stores that in a new column Count.

source = newTable(
stringCol("X", "A", "B", "A", "C", "B", "A", "B", "B", "C"),
stringCol("Y", "M", "N", "O", "N", "P", "M", "O", "P", "M"),
intCol("Number", 55, 76, 20, 130, 230, 50, 73, 137, 214),
)

result = source.countBy("Count", "X", "Y")