GROUP

Choose the columns your answer is grouped by.

Syntax

GROUP is a clause of LEARN. List the columns in a list of quoted names.

GROUP(["alias.column", "joined_table.column"])

Parameters and arguments

  • list

    Quoted column names. A column of the FROM table is written with its alias, such as "b.contentType", and a column of a joined table with that table's name, such as "content_types.name"

    Required: At least one, when RETURN has a plain column

Examples

Count the navigation links by whether they are published.

Return type and behaviour

LEARN returns one row for each distinct combination of the GROUP columns. Every plain column in RETURN must be one of them, and everything else must be inside an aggregate. A RETURN with only the group columns lists the combinations.

Errors and exceptions
ErrorWhat happenedHow to recover
HTQL-2001A column is in RETURN but not in GROUP, and it is not inside an aggregateAdd it to GROUP, or wrap it in COUNT, SUM, MAX, MIN or AVERAGE
Limitations
  • GROUP's names are the tables' columns, not RETURN's names.
  • Groups have no fixed order. Use SORT.
  • A column left out of GROUP and not inside an aggregate is refused, so no silent extra group appears.
  • Not supported: EXPR("…") as a group key. Group by a column.
Best practices
  • Group by the fewest columns that answer your question.
  • Sort the groups so the answer reads in a steady order.