﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
6821	Aggregate support for QuerySets	nicolaslara@…	Nicolas Lara	"Add aggregate support for !QuerySets according to what have been discussed in http://groups.google.com/group/django-developers/browse_thread/thread/3d4039161f6d2f5e/302b088cc816c0a6?hl=en#302b088cc816c0a6

The proposed syntax includes the two !QuerySet modifiers _annotate_ and _aggregate_ to augment the objects with aggregate information and retrieve aggregate information regarding the whole !QuerySet respectively. 

The notation for defining the fields and aggregate functions to use would be done in filter-like syntax
{{{
#!python
Purchase.objects.all().aggregate(quantity__sum='total_adquisitions', payment__avg='mean_pay') 
}}}
with the possibility of specifying aggregates in object form for non aliased aggregates (only the simple form is intended, for a more complex approach to object aggregates: http://code.djangoproject.com/ticket/6811 ). In this case a standard alias should be generated (i.e. quantity_sum, payment_avg)
{{{
#!python
Purchase.objects.all().aggregate(Sum('quantity'), Avg('payment'))
}}}

The GROUP BY is to be done internally. To specify the fields to be grouped it could be done with values()
{{{
#!python
Purchase.objects.all().values('identifier').aggregate(quantity__sum='num_adquisiitions') 
}}}

No way to specify that only the aggregated fields should appear in the results is intended because this could be done with the modifiers proposed in [#5420]. 

Ordering should be specified using order_by standard syntax with the aliased name.

==Extra ==

As extra things that could be implemented are:
 * Custom aggregate creation (possibly as follows)
{{{
#!python
>>> my_own_aggregate = Aggregate( sum('field_1') * 'field2', 'default_name')
>>> queryset.aggregate(purchases__price__custom=my_own_aggregate('alias')) 
}}}
 * F syntax for refering to other fields in the model.

Please refer to the discussion thread for a more in depth description"		closed	Database layer (models, ORM)	queryset-refactor		duplicate	aggregation		Unreviewed	1	0	0	1	0	0
