My issue is similar to this question: How to select only the latest rows for each user? But I am implementing this with Django.
In the following example (which I borrowed from the question above), I need to extract only the last row for each user. Additionally, in my case, I only want to get rows for a particular list of user_ids.
id | user_id | period_id | completed_on
----------------------------------------
1 | 1 | 1 | 2010-01-01
2 | 2 | 1 | 2010-01-10
3 | 3 | 1 | 2010-01-13
4 | 1 | 2 | 2011-01-01
5 | 2 | 2 | 2011-01-03
6 | 2 | 3 | 2012-01-13
... | ... | ... | ...
If the user_list is [1, 2], I'd like to get a result like this:
id | user_id | period_id | completed_on
----------------------------------------
4 | 1 | 2 | 2011-01-01
6 | 2 | 3 | 2012-01-13
I was writing it using filter, but couldn't figure out the right way.
PeriodTable.objects.filter(user__in=user_list, period_id=max(....?)).values(...)