Hi, I have a list of "news" matches for certain companies, ordered by the date they were found. I want to grab 5 results for each company up to 5 companies. I initially approached this problem by just grabbing a large amount of the newest results (LIMIT 100 or so) and then, through python, grouping them into lists for each company. However, this has a pitfall in that if one company has most of the results for the last 100 newest results, I will only have one company in the final set, rather than 5.
I thought I might be able to use DISTINCT query and do something like Match.objects.filter(type='company').values('contact_id__company').order_by('- added_date').distinct()[:5] To get the newest 5 companies mentioned, and then take those names and do some more queries for each company. But that has a failure that I've read about in the docs (http://docs.djangoproject.com/en/dev/ topics/db/aggregation/#interaction-with-default-ordering-or-order-by) I also tried an annotate method like this: Match.objects.filter(client_id__pk=42, result_type='company').order_by('- added_date').values('contact_id__company').annotate().order_by('contact_id__company') But the problem with this is that I can't LIMIT after I sort by order, so I end up just getting a DISTINCT list of all companies that have Match objects for them. Is there another way I could do this, where I'm guaranteed at least 5 distinct company names sorted by the date news was found for them? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.