Question

Is there some way to GROUP BY in App Engine, via GQL or Query methods?

I made some research and I've read that there isn't GROUP BY in GQL, and looks like there isn't also in Query class.

If is there any resource or any approach to implement with Python, please let me know.


EDIT

I've read also that its possible to make a GROUP BY with Map Reduce. If anyone has experience with this, a help would be accepted.

Thanks.

Was it helpful?

Solution

No, the datastore does not support any kind of database-side processing or aggregation. The solution is to do all post processing in memory. The two ways to do that are in each request, or with map reduce. Either solution will always return all rows in your query, but how you get the rows varies.

OTHER TIPS

You can't perform GROUP BY queries with appengine libraries. This is a failing of NoSQL database architectures, in gereral (of which Google's BigTable is one implementation).

Depending on what you might be doing with GROUP BY, an alternative approach may suit your needs. For each "type" of model you are querying your GROUP BY against, build a db.Model class to represent each one. Then query those classes directly.

Alternatively, if you have foreknowledge of the values of the returned groups, you could Query#filter() for each of those values, and treat those queries as your GROUP BYed sets.

I guess the solution ultimately depends on what you plan on doing with the grouped data. If the grouping requirement is to simplify output in the HTML code, then you can solve that by regrouping the data in the template.

For calculations or some other kind of aggregation you would need to do that in memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top