I am trying to get all the last records based on the id, sorted by the month.
this gives me the last record,
qs = Cashflow.objects.filter ( acct_id = a.id ).order_by('-month')[:1]
And this groups the accounts,
qs = Cashflow.objects
.filter ( acct_id = a.id )
.values ( 'acct_id' )
.annotate ( count = Count ( 'acct_id' ) )
.values ( 'acct_id', 'count' )
.order_by ( )
How how can I combine the two queries into one?
Group by acct_id, sort by "month" and get last record.
is this even possible? thanks
EDIT:
this is the sql version of what I am trying to do.
select *
from cashflow t
inner join (
select acct_id, max ( `month` ) as MaxDate
from cashflow
where acct_id IN ( 1,2,3,... )
group by acct_id
) tm on t.acct_id = tm.acct_id and t.month = tm.MaxDate
order by acct_id
Can this be done in pure Django of should I just do a Raw query?
cheers.