Re: how to get count of each distinct value

2013-11-22 Thread Fatih Tiryakioglu
Thank you.


--

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 
> 
> > 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)10
>>
>> I 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+unsubscr...@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.


Re: how to get count of each distinct value

2013-11-22 Thread Larry Martell
On Fri, Nov 22, 2013 at 10:44 AM, Fatih Tiryakioglu 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)10
>
> I 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+unsubscr...@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/CACwCsY4nhWaHgENp0Rp%3D%3DVRgXOhE_uFjb_H8AkwkDADuHUHq5g%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


how to get count of each distinct value

2013-11-22 Thread Fatih Tiryakioglu
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)10

I 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.


--

-- 
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+unsubscr...@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/301f4583-892e-4e06-b3a7-983381b95fbd%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.