Monday, May 4, 2015

Re: Fields outside Aggregation functions

I didn't quite understand have I sent the answer to Somecallitblues...
So write it once more

Forgot about Stores...
Let's look at Publisher and Book (those of Django documentation described)
Here is table beneath for example:
Publisher
Publisher-a
Publisher-b
Book
id
2 book_1 1$ Publisher-a
7 book_2 2$ Publisher-b
9 book_3 0,5$ Publisher-a
14 book_2 4$ Publisher-b

If I'll write something like
Publisher.objects.annotate(pr_max=Max('book__price'))
I'll get (as I understand):
Publisher       pr_max
Publisher-a;     1$
Publisher-b;     4$

But I want to see wich book was selected by Max() function:
Publisher       pr_max    book_id
Publisher-a;     1$;           2
Publisher-b;     4$;          14


On Monday, May 4, 2015 at 1:48:30 PM UTC+3, Alex-droid AD wrote:


There is example in Django documentation about using Aggregate and Annotate clauses

The same rules apply to the aggregate() clause. If you wanted to know the lowest and highest price of any book that is available for sale in a store, you could use the aggregate:

Store.objects.annotate(min_price=Min('books__price'), max_price=Max('books__price'))
Store.objects.aggregate(min_price=Min('books__price'), max_price=Max('books__price'))

By this string of code I'll get min/max price each of book in each store
But I won't see name or id of book, which was selected during use of aggregation (max or min)
So the question... How can I get records

store    min/max price    book ???





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/409e63a3-e2c1-4b48-a417-3bb69e3d860a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment