Re: how to get count of each distinct value
Thank you.
-- --
22 Kasım 2013 Cuma 18:07:47 UTC+2 tarihinde Larry....@gmail.com yazdı:
22 Kasım 2013 Cuma 18:07:47 UTC+2 tarihinde Larry....@gmail.com yazdı:
On Fri, Nov 22, 2013 at 10:44 AM, Fatih Tiryakioglu <fati...@gmail.com> wrote:Hi all,How can I get distribution of values in a field? For example, I have the scores below:(score)(raw1) 1(raw2) 1(raw3) 3(raw4) 4(raw5) 4(raw6) 5(raw7) 8(raw8) 8(raw9) 9(raw10) 10I want to have a result like that: {1: 2}, {3: 1}, {4: 2}, {5: 1}, {8: 2}, {9: 1}, {10: 1}. So it is {score: count}.How can I evaluate this only one db hit?Thanks all.In SQL:SELECT score, COUNT(*) FROM foo GROUP BY score;In Django:from django.db.models import Count
q = Foo.objects.values('score').annotate(Count('score')) for r in q:
print r, r.score__count
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/95c5956c-7ca3-4930-9ab4-25fa47b8d249%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home