Summing on only the most recently added record
I'm having a hard time wrapping my head around this Django query. I could probably figure this out with SQL (and maybe I'll have to), but I was wondering if there is a way to do it with Django objects.
The model looks something like this (simplified for clarity):
class Stat(model.Models):
entry_date = models.DateTimeField()
quantity = models.PositiveIntegerField()
user = models.ForeignKey(User)
I need to return the sum (using annotate and sum, I'm guessing) of all the most recently added quantities by a user, grouped by month. It's a little hard to explain, but the quantity is not cumulative -- I only need to deal with the most recent records for a given user and a given month, and then I need to sum those quantities, and group them by month. If more explanation is needed, please say so.
UPDATE:
Here's some rough psudo-code, as requested. This code is not necessarily what I would expect, but it's roughly what I could do if, but in a slow, programmatic way. I'm hoping there is a way to do this via a single query for the sake of speed. (BTW, this is based on Manoj Govindan's code below.)
year = list_of_months_that_hav开发者_C百科e_stats
users = all_users_in_database
for months in year:
for user in users:
sum = Stat.object.filter(user=user, entry_date__month=month).order_by('-entry_date')[0].aggregate(sum=Sum('quantity'))
final_output[month] = sum
Also, notice that I'm trying to get the very last record for the given month. I'm doing this with order_by, but as far as I know this won't work -- it's just an illustration.
The code above won't work, of course. The missing piece of the puzzle is the descending order_by that gets only the first item in the query.
I am not sure I understand exactly what you want. See my answer below and correct me if I am not getting it right.
I only need to deal with the most recent records for a given user and a given month, and then I need to sum those quantities, and group them by month.
(Emphasis added). If this is what you need, then you can use filter
in combination with aggregate
, as shown below.
from django.db.models import Sum
q = Stat.objects.filter(user = user, entry_date__month = 10).aggregate(
sum = Sum('quantity'))
print q
# {'sum': 14}
The filter conditions ensure that you have the required user
and that the month
part of the entry_date
matches the month you want (in this case October, therefore 10
). You can then Sum
the quantity for the user/month combination.
Update
If you want the sum for an user per each month (I am guessing here that this is what you meant by "group them by month") then you can try something like this:
select = dict(month = 'extract(month from entry_date)')
q = Stat.objects.filter(user = user).extra(
select = select).values('month').annotate(
sum = Sum('quantity'))
print q
# [{'sum': 14, 'month': 10.0}, {'sum': 6, 'month': 9.0}]
Here is a quick explanation.
extra
is used to specify that you want to extract the month part from the date. This is database specific. In my case the syntaxextract(month from entry_date)
is specific to Postgresql.Use
values
to mention that you only needmonth
. This is important. If you leave out thevalues
part all fields are fetched and you won't get the effect you need.Finally
annotate
each record with theSum('quantity')
. This returns the sum for each month.
精彩评论