Question

I have a data store in the app on Google App Engine, in each row it has a number and a datetimeproperty information.

In the application what I am trying to do is to tell the average of the numbers in a weekday,

e.g. sample data in data store

num          date
100          2013-10-16 21:04:17.809070
467          2013-10-13 21:19:19.014730
200          2013-10-09 22:19:20.015630

And since 2013-10-16 and 2013-10-09 are Wednesdays, I want a result something like

150          

How to do this in Gql?

Was it helpful?

Solution

There is no way to do this directly in GQL as aggregate functions (SUM, AVG, etc) are not supported in the datastore. Your best bet is to do this manually -- issue a query for the dates that you want (using a projection here for the number could be a good idea), and then average them yourself. Careful though -- if you have a lot of data this will be slow, and eventually will take too long for each request.

It may also make sense for you to keep track of these stats separately. When you added entities you would also have to update the stats. See this page about sharded counters.

Another option if you are doing batch processing is to run this either in a backend or in a map reduce. How up-to-date the numbers you get will depend on how often you run the job, but it will scale. Here is a link to the map reduce documentation.

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