////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////
:documentationPath: /pipeline/transforms/
:language: en_US
:description: The Group By transform groups rows from a source, based on a specified field or collection of fields. A new row is generated for each group.

= image:transforms/icons/groupby.svg[Group By transform Icon, role="image-doc-icon"] Group By

[%noheader,cols="3a,1a", role="table-no-borders" ]
|===
|
== Description

The Group By transform groups rows from a source, based on a specified field or collection of fields. A new row is generated for each group.

It can also generate one or more aggregate values for the groups.

Common uses are calculating the average sales per product and counting the number of an item you have in stock.

The Group By transform is designed for sorted inputs.

If your input is not sorted, only double consecutive rows are grouped correctly.

If you sort the data outside of Hop, the case sensitivity of the data in the fields may produce unexpected grouping results.

You can use the xref:pipeline/transforms/memgroupby.adoc[Memory Group By] transform to handle non-sorted input.

|
== Supported Engines
[%noheader,cols="2,1a",frame=none, role="table-supported-engines"]
!===
!Hop Engine! image:check_mark.svg[Supported, 24]
!Spark! image:check_mark.svg[Supported, 24]
!Flink! image:check_mark.svg[Supported, 24]
!Dataflow! image:check_mark.svg[Supported, 24]
!===
|===

== Options

[options="header"]
|===
|Option|Description
|Transform name|Name of the transform.
|Include all rows?|Enable if you want all rows in the output, not just the aggregation; to differentiate between the two types of rows in the output, a flag is required in the output.
You must specify the name of the flag field in that case (the type is boolean).
|Temporary files directory|The directory in which the temporary files are stored (needed when the Include all rows option is enabled and the number or grouped rows exceed 5000 rows); the default is the standard temporary directory for the system
|TMP-file prefix|Specify the file prefix used when naming temporary files
|Add line number, restart in each group|Enable to add a line number that restarts at 1 in each group
|Line number field name|The name of the field added to contain the line numbers.
|Always give back a row|If you enable this option, the Group By transform will always give back a result row, even if there is no input row.
This can be useful if you want to count the number of rows.
Without this option you would never get a count of zero (0).
|Group fields table|Specify the fields over which you want to group.
Click Get Fields to add all fields from the input stream(s).
|Aggregates table a|Specify the fields that must be aggregated, the method and the name of the resulting new field.
Here are the available aggregation methods :

- Sum
- Average (Mean)
- Median
- Percentile
- Minimum
- Maximum
- Number of values (N)
- Concatenate strings separated by , (comma)
- First non-null value
- Last non-null value
- First value (including null)
- Last value (including null)
- Cumulative sum (all rows option only!)
- Cumulative average (all rows option only!)
- Standard deviation (population)
- Concatenate strings separated by <Value>: specify the separator in the Value column (This supports xref::variables.adoc#_hexadecimal_values[hexadecimals])
- Number of distinct values
- Number of rows (without field argument)
- Standard deviation (sample)
- Percentile (nearest-rank method)
- Concatenate string separated by new line (CRLF)
- Concatenate distinct values separated by <Value>: specify the separator in the Value column (This supports xref::variables.adoc#_hexadecimal_values[hexadecimals])
|===