Group By and Aggregated Values #416
Comments
Comment by ejoebstl Hello Soren, |
Comment by ejoebstl For the multiple level group, can you please add example data (ungrouped as well as grouped)? I can't quite grasp the concept of multi-level groups. |
Comment by sorenbs @ejoebstl I have added example responses to all queries. This should make the proposed dynamics very clear :-) Looking forward to your feedback. The multi level groups are really very simple. By exploiting the fact that we have a wonderful tree structure to place data into. The more interesting question is wether this is useful or not. |
Comment by ejoebstl It's an excellent idea to allow grouping without aggregation by exploiting the three structure. That's a main limitation of SQL. The feature itself is very useful. Until now, when you wanted to group data, you needed to come up with either a relation or do it in your application. Grouping and aggregation is not only incredibly useful for building powerful frontends (think of a search feature for thousands of nodes, where you can filter by fields), but also decreases overhead in the backend by a lot. Even if I just want to gather some statistics about my data using the playground, this makes everything easier. Some considerations:
I'm quite sure the proposal is a good way though. The few points above can most likely be added afterwards without any complication. |
Comment by sorenbs
Correct. It's also not possible to use an arbitrary expression. I think this ability might be worth giving up in trade for a simple type-safe API
See proposal #1279
Great idea!
In the future there will be only a single API flavour as described in #353 |
Comment by nikolasburk There is no example for a {
postsConnection {
aggregate {
count
}
}
} Please confirm or correct! |
Comment by kieusonlam Is it possible to order by aggregated value? I try to do a something like:
I want to query top Course order by daily / weekly / ... views. It will sum all episiodes views between 2 date and order by that sum. |
Sorry, this issue shouldn't have been created :) Please see the discussion in https://github.com/graphcool/prisma/issues/1312 as well. |
link is not working anymore.. :/ |
|
@marktani I'm wondering how'd deal with a large number of groups, so large that you kinda want to implement some sort of pagination in the groupBy itself. Let's say you goupBy date, in total you have 10 000 entitities and each data contians 2 entities. So if you groupBy date, you'd get a list of 5000 keys. How would the query look like (notice the edges under city instead of being able to get the key directly)? {
allUsersConnection {
groupBy {
city {
edges {
node {
key
connection {
edges {
node {
id
name
}
}
}
}
}
}
}
}
} |
Issue by sorenbs
Tuesday Nov 21, 2017 at 11:26 GMT
Originally opened as https://github.com/graphcool/prisma/issues/1312
#70 Was a wide ranging discussion of how to support GroupBy and Aggregations in a type safe GraphQL API. This issue takes the learnings from previous discussions and provides a final API Proposal.
Throughout this proposal the examples will be based on this data schema:
Retrieving all users who live in
Aarhus
:See example return value
Data:
Return value:
Aggregations
Aggregate functions
API
Getting the average age of people living in
Aarhus
is accomplished like this in SQL:With Graphcool it would look like this:
See example return value
Data:
Return value:
Limiting the scope of aggregations
The normal
filter
,skip
,first
andorderBy
arguments can be used to limit the scope of data included in the aggregations:This will return the average age of the 5 oldest people in
Aarhus
See example return value
Data:
Return value:
Larger example
combining aggregations and data retrieval:
See example return value
Data:
Return value:
Group
In relational databases,
GROUP BY
is most often used together with aggregation functions like thisSELECT city, AVG(age) FROM User GROUP BY city
Because GraphQL returns tree structured data, it is quite compelling to use groupBy without aggregation functions:
See example return value
Data:
Return value:
Or even in multiple levels:
See example return value
Data:
Return value:
Combining groupBy and aggregations
The following query will group by city, return first 5 Users, average age of first 5 users and average age of everyone in city
See example return value
Data:
Return value:
Limitations
Both
groupBy
and aggregations are on single fields only. You can filter the data that goes into the aggregation, but there is no way to use expressions as keys in a group by query.The text was updated successfully, but these errors were encountered: