Re: percentage of models given field value?
On Nov 24, 2010, at 4:37 PM, Lachlan Musicman wrote:
> Using django 1.2 I wanted to present some statistics on the data that
> we have been collecting in our db.
>
> We have source texts with authors, and target texts with their translators.
>
> The target text model has a value "Language".
>
> I get the count of the target texts: all, and then filtered by
> language. How would I present each language as a percentage of the
> total target text count?
This is a great query to write as SQL, rather than using the ORM (I love the Django ORM, but the right tools for the right job).
Assuming more or less standard Django names, you could return a list of the languages, the total number of texts for each language, and the percentage of the total with something along the lines of:
SELECT language, COUNT(*), (COUNT(*)/(SELECT COUNT(*) FROM app_targettexts))
FROM app_targettexts
GROUP BY language
This has the advantage that it only hits the database once. If you need the grand total, you can either calculate it in the application, or thorw on:
UNION
SELECT 'total', COUNT(*), 1.0 FROM app_targettexts
--
-- Christophe Pettus
xof@thebuild.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home