is there any max() function available in appengine datastore to find max of selected records

155 views
Skip to first unread message

pyrocks

unread,
Mar 13, 2010, 9:48:22 AM3/13/10
to Google App Engine
HI good morning,


i am very new to the appengine.
anybody help me In finding the maximum salary from the employee table
in appengine datastore.

here shall we have any max() function available like in oracle to find
max salaried employee.


Thanks and regards.

Greg

unread,
Mar 14, 2010, 10:00:48 PM3/14/10
to Google App Engine
On Mar 14, 3:48 am, pyrocks <kiranbe...@gmail.com> wrote:
> here shall we have any max() function available like in oracle to find
> max salaried employee.

In a word, no. You need to store the maximum salary in the datastore,
and every time time a salary changes check to see if it exceeds the
maximum, and update it if so.

Takashi Matsuo

unread,
Mar 14, 2010, 10:12:50 PM3/14/10
to google-a...@googlegroups.com
Hi,

Perhaps you can order by salary property descending, and just get the
first one, for getting max value.

max_salaried_employee = Employee.all().order("-salary").get()
max_salary = max_salaried_employee.salary

hope this helps

--
Takashi Matsuo
Kay's daddy

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Eli Jones

unread,
Mar 14, 2010, 10:43:58 PM3/14/10
to google-a...@googlegroups.com
Like Takashi says.. you can effectively get the max() for a numeric property (db.FloatProperty() or db.IntegerProperty()) by issuing a command like he gave.. or a more SQL looking query like this:

result = db.GqlQuery("Select * from Employee Order By salary desc").fetch(1)
maxsalary = result[0].salary

Other built in SQL functions like Sum() aren't really doable though. (Not in a simple way).

Max

unread,
Mar 15, 2010, 10:50:28 PM3/15/10
to Google App Engine
Order by salary means an extra index will be built over this field,
which will affect your write speed.

Use a global variable to store max(salary)

On Mar 15, 10:12 am, Takashi Matsuo <matsuo.taka...@gmail.com> wrote:
> Hi,
>
> Perhaps you can order by salary property descending, and just get the
> first one, for getting max value.
>
> max_salaried_employee = Employee.all().order("-salary").get()
> max_salary = max_salaried_employee.salary
>
> hope this helps
>
> --
> Takashi Matsuo
> Kay's daddy
>

Ulrich

unread,
Mar 16, 2010, 3:01:47 PM3/16/10
to google-a...@googlegroups.com
Max wrote:
> Order by salary means an extra index will be built over this field,
> which will affect your write speed.
>
> Use a global variable to store max(salary)
>
If you store this global variable in the datastore and have a high
number of inserts, you have to update this global variable too often and
it will not work without some kind of sharding.

-Ulrich

Jeff Schnitzer

unread,
Mar 16, 2010, 3:35:40 PM3/16/10
to google-a...@googlegroups.com
On Tue, Mar 16, 2010 at 12:01 PM, Ulrich <miere...@googlemail.com> wrote:
> Max wrote:
>>
>> Use a global variable to store max(salary)
>
> If you store this global variable in the datastore and have a high number of
> inserts, you have to update this global variable too often and it will not
> work without some kind of sharding.

Even if there are zillions of salaries and they are constantly
changing, it's still unlikely that max(salary) is going to change
often enough for this to be a problem.

Jeff

Reply all
Reply to author
Forward
0 new messages