From: "Russell Keith-Magee" <freakboy3...@gmail.com>
Date: Fri, 13 Apr 2007 11:11:00 +0800
Subject: Re: Aggregates: anything planned for 1.0?
On 4/13/07, Honza Král <honza.k...@gmail.com> wrote:
> the queryset refactoring must (well, should) happen before aggregation Ok; I've had a chance to look at this now. Although there are some > support. After the refactoring, it should be relatively easy to put in > the aggregations. syntax differences, I think your idea and mine [1] are acutally pretty close. A bit of both, and we might just have something. Comments: GROUP BY = values() Isn't the group_by clause duplicating what is already provided by the So, your example would become: >>> queryset = Model.objects.all() 'some_other_field'), avg=('pay', 'age'), count=True) >>> queryset.values('name', 'city').aggregates(sum=('pay', Output format [ To me, the clause is returning a list of data groups (i.e., the Weaknesses in the function=tuple argument approach While the function=tuple of field names approach does work, I feel it The approach I proposed was to mirror the filter syntax: This approach mirrors the filter mechanism for attribute naming and The only downside I can see is that the simple case requires the user However, it should be possible to combine both approaches: >>> Model.objects.aggregate(sum=('pay'), other_field__average='mean_value') [ { 'name' : 'John Doe', 'city' : 'Prague', 'sum' :' { 'pay' : 50000, } 'mean_value' :30, }, ... ] This means the sum of pay isn't aliased, so it can't be filtered, but Ambiguities in COUNT aggregate(count=True) is already covered with count(); I'm not sure Author.objects.aggregate(books_count='number_of_books_written') On top of this, you can get the gross count from: len(Author.objects.aggregate(...)) Aggregate of results vs aggregates of related objects If you run an aggregate over a field from the table you are querying, - Get me the date of the earliest and latest published article: Return The second case is relatively easy: >>> Author.objects.aggregate(article__pub_date__min='earliest', article__pub_date__max='latest')) [{ 'name':'John Doe', 'earliest': 2007-01-04 'latest': 2007-02-14 } ... ] However, the first case: >>> Article.objects.aggregate(pub_date__min='earliest', pub_date__max='latest')) {[ 'title': 'first article' 'earliest': 2007-01-04 'latest': 2007-02-14 ], [ 'title': 'second article' 'earliest': 2007-01-04 'latest': 2007-02-14 ], } while legal, isn't the most elegant way at getting at a unique min/max for an entire table. This was the reason for my suggestion for two functions: aggregate and annotate. Aggregate returns a dictionary of just aggregated values: >>> Article.objects.aggregate(min=('pub_date'), max=('pub_date')) { 'min' : '2007-01-04', 'max' : '2007-02-14' } Whereas annotate() returns a list of annotated dictionary data. ================================ I'd better stop now - this is getting longer than I originally Yours, [1] http://groups.google.com/group/django-developers/msg/2f91fa217bc465bb You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||