How do I find the maximum value in a particular column of a table in the GAE datastore using GQL?
In SQL Server there are several ways to get the MIN or MAX of multiple columns including methods using UNPIVOT, UNION, CASE, etc… However, the simplest method is by using FROM … VALUES i.e. table value constructor. Let's see an example. In this example, there is a table for items with five columns for prices.
We used the MAX() function within a subquery to find the maximum value, and returned the whole row with the outer query.
To get the max you using GQL, you could do this:
max_x = db.GqlQuery("SELECT * FROM MyModel ORDER BY x DESC").get().x
You could use this syntactically shorter but equivalent approach:
max_x = MyModel.all().order('-x').get().x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With