Group

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

EdgeQL supports a top-level group statement. These are used to partition sets into subsets based on some parameters. These subsets then can be additionally aggregated to provide some analytics.

The most basic format is just using the bare group to group a set of objects by some property:

  1. db>
  1. group Movie 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. }

Notice that the result of group is a set of free objects with three fields:

  • key: another free object containing the specific value of the grouping parameter for a given subset.

  • grouping: set of names of grouping parameters, i.e. the specific names that also appear in the key free object.

  • elements: the actual subset of values that match the key.

In the group statement referring to the property in the by clause must be done by using the leading dot shothand .release_year. The property name then shows up in grouping and key to indicate the defining characteristics of the particular result. Alternatively, we can give it an alias in an optional using clause and then that alias can be used in the by clause and will appear in the results:

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

The using clause is perfect for defining a more complex expression to group things by. For example, instead of grouping by the release_year we can group by the release decade:

  1. db>
  2. ...
  3. ...
  1. group Movie {title}
  2. using decade := .release_year // 10
  3. by decade;
  1. {
  2. {
  3. {
  4. key: {decade: 200},
  5. grouping: {'decade'},
  6. elements: {
  7. default::Movie {title: 'Spider-Man'},
  8. default::Movie {title: 'Spider-Man 2'},
  9. default::Movie {title: 'Spider-Man 3'},
  10. default::Movie {title: 'Iron Man'},
  11. default::Movie {title: 'The Incredible Hulk'},
  12. },
  13. },
  14. {
  15. key: {decade: 201},
  16. grouping: {'decade'},
  17. elements: {
  18. default::Movie {title: 'Iron Man 2'},
  19. default::Movie {title: 'Thor'},
  20. default::Movie {title: 'Captain America: The First Avenger'},
  21. default::Movie {title: 'The Avengers'},
  22. default::Movie {title: 'Iron Man 3'},
  23. default::Movie {title: 'Thor: The Dark World'},
  24. default::Movie {title: 'Captain America: The Winter Soldier'},
  25. default::Movie {title: 'Ant-Man'},
  26. default::Movie {title: 'Captain America: Civil War'},
  27. default::Movie {title: 'Doctor Strange'},
  28. default::Movie {title: 'Spider-Man: Homecoming'},
  29. default::Movie {title: 'Thor: Ragnarok'},
  30. default::Movie {title: 'Ant-Man and the Wasp'},
  31. default::Movie {title: 'Spider-Man: No Way Home'},
  32. },
  33. },
  34. {
  35. key: {decade: 202},
  36. grouping: {'decade'},
  37. elements: {default::Movie {title: 'Black Widow'}},
  38. },
  39. }

It’s also possible to group by more than one parameter, so we can group by whether the movie title contains a colon and the decade it was released. Additionally, let’s only consider more recent movies, say, released after 2015, so that we’re not overwhelmed by all the combination of results:

  1. db>
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...
  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
  6. decade := .release_year // 10,
  7. has_colon := .title like '%:%'
  8. by decade, has_colon;
  1. {
  2. {
  3. key: {decade: 201, has_colon: false},
  4. grouping: {'decade', 'has_colon'},
  5. elements: {
  6. default::Movie {title: 'Ant-Man and the Wasp'},
  7. default::Movie {title: 'Doctor Strange'},
  8. },
  9. },
  10. {
  11. key: {decade: 201, has_colon: true},
  12. grouping: {'decade', 'has_colon'},
  13. elements: {
  14. default::Movie {title: 'Captain America: Civil War'},
  15. default::Movie {title: 'Spider-Man: No Way Home'},
  16. default::Movie {title: 'Thor: Ragnarok'},
  17. default::Movie {title: 'Spider-Man: Homecoming'},
  18. },
  19. },
  20. {
  21. key: {decade: 202, has_colon: false},
  22. grouping: {'decade', 'has_colon'},
  23. elements: {default::Movie {title: 'Black Widow'}},
  24. },
  25. }

Once we break a set into partitions, we can also use aggregate functions to provide some analytics about the data. For example, for the above partitioning (by decade and presence of : in the title) we can calculate how many movies are in each subset as well as the average number of words in the movie titles:

  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. decade := .release_year // 10 - 200,
  8. has_colon := .title like '%:%'
  9. by decade, has_colon
  10. )
  11. select groups {
  12. key := .key {decade, has_colon},
  13. count := count(.elements),
  14. avg_words := math::mean(
  15. len(str_split(.elements.title, ' ')))
  16. };
  1. {
  2. {key: {decade: 1, has_colon: false}, count: 2, avg_words: 3},
  3. {key: {decade: 1, has_colon: true}, count: 4, avg_words: 3},
  4. {key: {decade: 2, has_colon: false}, count: 1, avg_words: 2},
  5. }

It is possible to produce results that are grouped in multiple different ways using grouping sets. This may be useful in more sophisticated analytics.

See also

Reference > Commands > Group