Group

The group statement is only available in EdgeDB 2.0 or later.

group–partition a set into subsets based on one or more keys

  1. [ with with-item [, ...] ]
  2. group [alias := ] expr
  3. [ using using-alias := expr, [, ...] ]
  4. by grouping-element, ... ;
  5. where a grouping-element is one of
  6. ref-or-list
  7. { grouping-element, ... }
  8. ROLLUP( ref-or-list, ... )
  9. CUBE( ref-or-list, ... )
  10. where a ref-or-list is one of
  11. ()
  12. grouping-ref
  13. ( grouping-ref, ... )
  14. where a grouping-ref is one of
  15. using-alias
  16. .field-name

group expr

The group clause sets up the input set that will be operated on.

Much like in select it is possible to define an ad-hoc alias at this stage to make referring to the starting set concisely.

using using-alias := expr

The using clause defines one or more aliases which can then be used as part of the grouping key.

If the by clause only refers to .field-name the using clause is optional.

by grouping-element

The by clause sepecifies which parameters will be used to partition the starting set.

There are only two basic components for defining grouping-element: references to using-alias defined in the using clause or by references to the short-path format of .field-name. The .field-name has to refer to properties or links immediately present on the type of starting set.

The basic building blocks can also be combined by using parentheses ( ) to indicate that partitioning will happen based on several parameters at once.

It is also possible to specify grouping sets, which are denoted using curly braces { }. The results will contain different partitioning based on each of the grouping set elements. When there are multiple top-level grouping-elements then the cartesian product of them is taken to determine the grouping set. Thus a, {b, c} is equivalent to {(a, b), (a, c)} grouping sets.

ROLLUP and CUBE are a shorthand to specify particular grouping sets. ROLLUP groups by all prefixes of a list of elements, so ROLLUP (a, b, c) is equivalent to {(), (a), (a, b), (a, b, c)}. CUBE groups by all elements of the power set, so CUBE (a, b) is equivalent to {(), (a), (b), (a, b)}.

Output

The group statement partitions a starting set into subsets based on some specified parameters. The output is organized into a set of free objects of the following structure:

  1. {
  2. "key": { using-alias := value [, ...] },
  3. "grouping": <set of keys used in grouping>,
  4. "elements": <the subset matching to the key>,
  5. }

“key”

The “key” contains another free object, which contains all the aliases or field names used as the key together with the specific values these parameters take for this particular subset.

“grouping”

The “grouping” contains a str set of all the names of the parameters used as the key for this particular subset. This is especially useful when using grouping sets and the parameters used in the key are not the same for all partitionings.

“elements”

The “elements” contains the actual subset of values that match the “key”.

Examples

Here’s a simple example without using any aggregation or any further processing:

  1. db>
  1. group Movie {title} by .release_year;
  1. {
  2. {
  3. key: {release_year: 2016},
  4. grouping: {'release_year'},
  5. elements: {
  6. default::Movie {title: 'Captain America: Civil War'},
  7. default::Movie {title: 'Doctor Strange'},
  8. },
  9. },
  10. {
  11. key: {release_year: 2017},
  12. grouping: {'release_year'},
  13. elements: {
  14. default::Movie {title: 'Spider-Man: Homecoming'},
  15. default::Movie {title: 'Thor: Ragnarok'},
  16. },
  17. },
  18. {
  19. key: {release_year: 2018},
  20. grouping: {'release_year'},
  21. elements: {default::Movie {title: 'Ant-Man and the Wasp'}},
  22. },
  23. {
  24. key: {release_year: 2019},
  25. grouping: {'release_year'},
  26. elements: {default::Movie {title: 'Spider-Man: No Way Home'}},
  27. },
  28. {
  29. key: {release_year: 2021},
  30. grouping: {'release_year'},
  31. elements: {default::Movie {title: 'Black Widow'}},
  32. },
  33. ...
  34. }

Or we can group by an expression instead, such as whether the title starts with a vowel or not:

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  1. with
  2. # Apply the group query only to more recent movies
  3. M := (select Movie filter .release_year > 2015)
  4. group M {title}
  5. using vowel := re_test('(?i)^[aeiou]', .title)
  6. by vowel;
  1. {
  2. {
  3. key: {vowel: false},
  4. grouping: {'vowel'},
  5. elements: {
  6. default::Movie {title: 'Thor: Ragnarok'},
  7. default::Movie {title: 'Doctor Strange'},
  8. default::Movie {title: 'Spider-Man: Homecoming'},
  9. default::Movie {title: 'Captain America: Civil War'},
  10. default::Movie {title: 'Black Widow'},
  11. default::Movie {title: 'Spider-Man: No Way Home'},
  12. },
  13. },
  14. {
  15. key: {vowel: true},
  16. grouping: {'vowel'},
  17. elements: {default::Movie {title: 'Ant-Man and the Wasp'}},
  18. },
  19. }

It is also possible to group scalars instead of objects, in which case you need to define an ad-hoc alias to refer to the scalar set in order to specify how it will be grouped:

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  1. with
  2. # Apply the group query only to more recent movies
  3. M := (select Movie filter .release_year > 2015)
  4. group T := M.title
  5. using vowel := re_test('(?i)^[aeiou]', T)
  6. by vowel;
  1. {
  2. {
  3. key: {vowel: false},
  4. grouping: {'vowel'},
  5. elements: {
  6. 'Captain America: Civil War',
  7. 'Doctor Strange',
  8. 'Spider-Man: Homecoming',
  9. 'Thor: Ragnarok',
  10. 'Spider-Man: No Way Home',
  11. 'Black Widow',
  12. },
  13. },
  14. {
  15. key: {vowel: true},
  16. grouping: {'vowel'},
  17. elements: {'Ant-Man and the Wasp'}
  18. },
  19. }

Often the results of group are immediately used in a select statement to provide some kind of analytical results:

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  9. ...
  10. ...
  11. ...
  12. ...
  13. ...
  14. ...
  1. with
  2. # Apply the group query only to more recent movies
  3. M := (select Movie filter .release_year > 2015),
  4. groups := (
  5. group M {title}
  6. using vowel := re_test('(?i)^[aeiou]', .title)
  7. by vowel
  8. )
  9. select groups {
  10. starts_with_vowel := .key.vowel,
  11. count := count(.elements),
  12. mean_title_length :=
  13. round(math::mean(len(.elements.title)))
  14. };
  1. {
  2. {starts_with_vowel: false, count: 6, mean_title_length: 18},
  3. {starts_with_vowel: true, count: 1, mean_title_length: 20},
  4. }

It’s possible to group by more than one parameter. For example, we can add the release decade to whether the title starts with a vowel:

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  9. ...
  10. ...
  11. ...
  12. ...
  13. ...
  14. ...
  15. ...
  16. ...
  1. with
  2. # Apply the group query only to more recent movies
  3. M := (select Movie filter .release_year > 2015),
  4. groups := (
  5. group M {title}
  6. using
  7. vowel := re_test('(?i)^[aeiou]', .title),
  8. decade := .release_year // 10
  9. by vowel, decade
  10. )
  11. select groups {
  12. key := .key {vowel, decade},
  13. count := count(.elements),
  14. mean_title_length :=
  15. math::mean(len(.elements.title))
  16. };
  1. {
  2. {
  3. key: {vowel: false, decade: 201},
  4. count: 5,
  5. mean_title_length: 19.8,
  6. },
  7. {
  8. key: {vowel: false, decade: 202},
  9. count: 1,
  10. mean_title_length: 11,
  11. },
  12. {
  13. key: {vowel: true, decade: 201},
  14. count: 1,
  15. mean_title_length: 20
  16. },
  17. }

Having more than one grouping parameter opens up the possibility to using grouping sets to see the way grouping parameters interact with the analytics we’re gathering:

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  9. ...
  10. ...
  11. ...
  12. ...
  13. ...
  14. ...
  15. ...
  16. ...
  17. ...
  1. with
  2. # Apply the group query only to more recent movies
  3. M := (select Movie filter .release_year > 2015),
  4. groups := (
  5. group M {title}
  6. using
  7. vowel := re_test('(?i)^[aeiou]', .title),
  8. decade := .release_year // 10
  9. by CUBE(vowel, decade)
  10. )
  11. select groups {
  12. key := .key {vowel, decade},
  13. grouping,
  14. count := count(.elements),
  15. mean_title_length :=
  16. (math::mean(len(.elements.title)))
  17. } order by array_agg(.grouping);
  1. {
  2. {
  3. key: {vowel: {}, decade: {}},
  4. grouping: {},
  5. count: 7,
  6. mean_title_length: 18.571428571428573,
  7. },
  8. {
  9. key: {vowel: {}, decade: 202},
  10. grouping: {'decade'},
  11. count: 1,
  12. mean_title_length: 11,
  13. },
  14. {
  15. key: {vowel: {}, decade: 201},
  16. grouping: {'decade'},
  17. count: 6,
  18. mean_title_length: 19.833333333333332,
  19. },
  20. {
  21. key: {vowel: true, decade: {}},
  22. grouping: {'vowel'},
  23. count: 1,
  24. mean_title_length: 20,
  25. },
  26. {
  27. key: {vowel: false, decade: {}},
  28. grouping: {'vowel'},
  29. count: 6,
  30. mean_title_length: 18.333333333333332,
  31. },
  32. {
  33. key: {vowel: false, decade: 201},
  34. grouping: {'vowel', 'decade'},
  35. count: 5,
  36. mean_title_length: 19.8,
  37. },
  38. {
  39. key: {vowel: true, decade: 201},
  40. grouping: {'vowel', 'decade'},
  41. count: 1,
  42. mean_title_length: 20,
  43. },
  44. {
  45. key: {vowel: false, decade: 202},
  46. grouping: {'vowel', 'decade'},
  47. count: 1,
  48. mean_title_length: 11,
  49. },
  50. }

See also

EdgeQL > Group