Index: tests/regressiontests/aggr_joins/__init__.py
===================================================================
Index: tests/regressiontests/aggr_joins/fixtures/initial_data.json
===================================================================
--- tests/regressiontests/aggr_joins/fixtures/initial_data.json	(revision 0)
+++ tests/regressiontests/aggr_joins/fixtures/initial_data.json	(revision 0)
@@ -0,0 +1,104 @@
+[
+    {
+        "pk": 1,
+        "model": "aggr_joins.category",
+        "fields": {
+            "name": "Cat 1",
+            "order": 2
+        }
+    },
+    {
+        "pk": 2,
+        "model": "aggr_joins.category",
+        "fields": {
+            "name": "Cat 2",
+            "order": 1
+        }
+    },
+    {
+        "pk": 1,
+        "model": "aggr_joins.group",
+        "fields": {
+            "name": "Group 1",
+            "category": 1
+        }
+    },
+    {
+        "pk": 2,
+        "model": "aggr_joins.group",
+        "fields": {
+            "name": "Group 2",
+            "category": 2
+        }
+    },
+    {
+        "pk": 3,
+        "model": "aggr_joins.group",
+        "fields": {
+            "name": "Group 3",
+            "category": 1
+        }
+    },
+    {
+        "pk": 4,
+        "model": "aggr_joins.group",
+        "fields": {
+            "name": "Group 4",
+            "category": 2
+        }
+    },
+    {
+        "pk": 1,
+        "model": "aggr_joins.registration",
+        "fields": {
+            "group": 1,
+            "type": 1,
+            "time": 1
+        }
+    },
+    {
+        "pk": 2,
+        "model": "aggr_joins.registration",
+        "fields": {
+            "group": 1,
+            "type": 2,
+            "time": 2
+        }
+    },
+    {
+        "pk": 3,
+        "model": "aggr_joins.registration",
+        "fields": {
+            "group": 2,
+            "type": 1,
+            "time": 4
+        }
+    },
+    {
+        "pk": 4,
+        "model": "aggr_joins.registration",
+        "fields": {
+            "group": 2,
+            "type": 2,
+            "time": 8
+        }
+    },
+    {
+        "pk": 5,
+        "model": "aggr_joins.registration",
+        "fields": {
+            "group": 3,
+            "type": 1,
+            "time": 16
+        }
+    },
+    {
+        "pk": 6,
+        "model": "aggr_joins.registration",
+        "fields": {
+            "group": 3,
+            "type": 2,
+            "time": 32
+        }
+    }
+]
Index: tests/regressiontests/aggr_joins/models.py
===================================================================
--- tests/regressiontests/aggr_joins/models.py	(revision 0)
+++ tests/regressiontests/aggr_joins/models.py	(revision 0)
@@ -0,0 +1,94 @@
+# coding: utf-8
+# test for fault in joins with aggregates
+from django.db import models
+
+class Category(models.Model):
+    name = models.CharField(max_length=100)
+    order = models.IntegerField()
+
+    def __unicode__(self):
+        return self.name
+
+class Group(models.Model):
+    category = models.ForeignKey(Category)
+    name = models.CharField(max_length=300)
+
+    def __unicode__(self):
+        return self.name
+
+class Registration(models.Model):
+    group = models.ForeignKey(Group)
+    type = models.SmallIntegerField()
+    time = models.IntegerField()
+
+__test__ = {'API_TESTS': """
+>>> from django.db.models import Sum
+
+# the categories are reversely ordered:
+>>> Category.objects.order_by('order')
+[<Category: Cat 2>, <Category: Cat 1>]
+
+# all groups order by category.order
+>>> qs = Group.objects.order_by('category__order').distinct()
+>>> [(group.id, group.category.id) for group in qs]
+[(2, 2), (4, 2), (1, 1), (3, 1)]
+
+# annotated
+>>> qs = Group.objects.annotate(sum_time = Sum('registration__time') \
+    ).order_by('id')
+>>> [(group.id, group.category.id, group.sum_time) for group in qs]
+[(1, 1, 3), (2, 2, 12), (3, 1, 48), (4, 2, None)]
+
+# annotated and ordered
+# this fails on PostGres because the ordering column is not included in the
+# GROUP BY
+>>> qs = Group.objects.annotate(sum_time = Sum('registration__time') \
+    ).order_by('category__order')
+>>> [(group.id, group.category.id, group.sum_time) for group in qs]
+[(2, 2, 12), (4, 2, None), (1, 1, 3), (3, 1, 48)]
+
+# filtered and ordered
+# this is a dummy filter, but it joins the tables so only the ones
+# with registrations return
+# requires distinct
+# also fails on postgres
+>>> qs = Group.objects.filter(registration__type__gt=0 \
+    ).order_by('category__order').distinct()
+>>> [(group.id, group.category.id) for group in qs]
+[(2, 2), (1, 1), (3, 1)]
+
+# filtered, annotated and ordered
+# this fails on postgres (missing in group by)
+# but also on sqlite: wrong result
+# I get this: [(4, 2, 256), (2, 2, 12), (1, 1, 3), (3, 1, 240)]
+# the result for group 4 includes all registrations
+>>> qs = Group.objects.filter(registration__type__gt=0 \
+    ).annotate(sum_time = Sum('registration__time') \
+    ).order_by('category__order')
+>>> [(group.id, group.category.id, group.sum_time) for group in qs]
+[(2, 2, 12), (1, 1, 3), (3, 1, 48)]
+>>> qs.query.as_sql()
+
+# notice how the inner join for the order_by comes after the left outer joins
+# putting the inner join first gives the correct result
+
+# filtered, annotated, but unordered
+>>> qs = Group.objects.filter(registration__type__gt=0 \
+    ).annotate(sum_time = Sum('registration__time') \
+    ).order_by('id')
+>>> [(group.id, group.category.id, group.sum_time) for group in qs]
+[(1, 1, 3), (2, 2, 12), (3, 1, 48)]
+
+# annotated but unordered and unfiltered
+>>> qs = Group.objects.annotate(sum_time = Sum('registration__time') \
+    ).order_by('id')
+>>> [(group.id, group.category.id, group.sum_time) for group in qs]
+[(1, 1, 3), (2, 2, 12), (3, 1, 48), (4, 2, None)]
+
+# filtered but unordered
+>>> qs = Group.objects.filter(registration__type__gt=0 \
+    ).order_by('id').distinct()
+>>> [(group.id, group.category.id) for group in qs]
+[(1, 1), (2, 2), (3, 1)]
+
+"""}
