Data Model: class A(models.model): desc: model.CharField() class B(models.model): a: model.ForeignKey('A') desc: models.CharField() I need to do this select in a view: select max(num_a) as max_num_a from (select a, count(desc) as num_a from B group by a) as x; I've tried this: result = B.objects.values('a').annotate(num_a=Count('a')).aggregate(Max('num_a')) And I got this error: 1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (SELECT `B`.a` AS `a`, COUNT(`a' at line 1") If we only group and not obtain max it works: result = B.objects.values('a').annotate(num_a=Count('a')) and the result is : {'a': 1L, 'num_a': 9}{'a': 2L, 'num_a': 6}{'a': 3L, 'num_a': 4} I'm not sure what I'm doing wrong, neither if there's other way to resolve it. If somebody have been faced with and resolved it, I'll be gratefull if he can show me how can I start for beating this. Thanks all.
-- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.