Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django queryset group by and count distincts

In my django application, I have a set of donation, which I want to make statistics on. For each donation, a person is linked with his email address.

I want to compute for each month, the number of donors, that is the number of unique email addess.

So far I have:

# filter all donations by month
truncate_date = connection.ops.date_trunc_sql('month', 'date')
qs = Donation.objects.extra({'month': truncate_date})

# group by months and annotate each with the count of emails
donors = qs.values('month').annotate(count=Count('email')).order_by('month')

This works well, I get a monthly report of all email donors, but as I got duplicate in email, it does not filter email by unique values.

The current database used is Postgresql.

How this can be done?

like image 624
nobe4 Avatar asked May 12 '16 09:05

nobe4


1 Answers

Count takes a distinct argument, so you can do:

donors = qs.values('month').annotate(count=Count('email', distinct=True)).order_by('month')
like image 161
solarissmoke Avatar answered Oct 07 '22 10:10

solarissmoke