Fields outside Aggregation functions

31 visualizações
Ir para a primeira mensagem não lida

Alex-droid AD

não lida,
04/05/2015, 06:48:3004/05/15
para django...@googlegroups.com


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 ???





Mario Gudelj

não lida,
04/05/2015, 09:11:2204/05/15
para django...@googlegroups.com

Can you perhaps order by price and get first and last items?

--
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...@googlegroups.com.
To post to this group, send email to django...@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/814ef5a3-6e2b-4054-9a1c-6ca127c2d527%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carsten Fuchs

não lida,
04/05/2015, 09:44:0004/05/15
para django...@googlegroups.com
Hi Alex,

Am 04.05.2015 um 12:48 schrieb Alex-droid AD:
> So the question... How can I get records
>
> store min/max price */_book ???_/*

I recently had the same question, please see this thread:
https://groups.google.com/forum/#!topic/django-users/adRe2_BWMz0

If you ever find a more direct solution than the one mentioned there, please let me know!

Best regards,
Carsten

Alex-droid AD

não lida,
04/05/2015, 10:00:2704/05/15
para django...@googlegroups.com
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
Responder a todos
Responder ao autor
Reencaminhar
0 mensagens novas