Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the maximum value in a column using GQL

How do I find the maximum value in a particular column of a table in the GAE datastore using GQL?

like image 575
Amarnath Ravikumar Avatar asked Oct 05 '10 18:10

Amarnath Ravikumar


People also ask

How do I get the maximum value from another column of a table in SQL?

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.

How do I get the highest row value in SQL?

We used the MAX() function within a subquery to find the maximum value, and returned the whole row with the outer query.


1 Answers

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
like image 195
David Underhill Avatar answered Oct 19 '22 19:10

David Underhill