﻿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
26067	Orderable ArrayAgg and StringAgg	Floris den Hengst	Floris den Hengst	"The Postgres-specific ArrayAgg and StringAgg aggregations were added in [https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/aggregates/ Django 1.9].

The [http://www.postgresql.org/docs/9.0/static/functions-aggregate.html documentation] of Postgres 9.0 first mentions the possibility of ordering the results within aggregations such as ARRAY_AGG and STRING_AGG.

This could be useful in some cases.
For example: it could make sense to perform a StringAgg in lexicographical order in some cases.

The basic format of ordering within aggregations in SQL is quite simple:
{{{
#!sql
SELECT ARRAY_AGG(some_field ORDER BY some_field ASC) FROM table;
SELECT ARRAY_AGG(some_field ORDER BY some_field DESC) FROM table;
SELECT ARRAY_AGG(some_field ORDER BY other_field ASC) FROM table;
}}}

It would be nice if the above would be supported as follows:
{{{
#!python
Model.objects.aggregate(ArrayAgg(some_field, order_by='some_field'))
Model.objects.aggregate(ArrayAgg(some_field, order_by='-some_field'))
Model.objects.aggregate(ArrayAgg(some_field, order_by='other_field'))
}}}

where order_by is an optional parameter. If it not specified, behavior can remain unchanged from the current implementation.

As noted by Josh Smeaton in the [https://groups.google.com/forum/#!topic/django-developers/YCSDX3GApBM mailinglist] discussion for this feature, any ordering added within '''may''' need to be contributed to GROUP BY. This might require some investigation."	New feature	closed	contrib.postgres	dev	Normal	fixed	ArrayAgg StringAgg ordering	Matthew Pava	Accepted	1	0	0	1	0	0
