Opened 9 years ago
Last modified 9 years ago
#25977 closed New feature
Allow different filters on multiple aggrigates — at Initial Version
Reported by: | Kyle Agronick | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.9 |
Severity: | Normal | Keywords: | QuerySet.extra |
Cc: | Simon Charette | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is the query I that appears to be impossible to implement using the ORM:
SELECT id, name, start_date,
(SELECT count(state) FROM jobs_job WHERE state = 'F' and job_group_id = jg.id GROUP BY state) as badcount ,
(SELECT count(state) FROM jobs_job WHERE state = 'C' and job_group_id = jg.id GROUP BY state) as goodcount
FROM jobs_jobgroup as jg
The main table represents a job group. The two queries tell how many successes and failures there are in the jobs for the job group. Currently, there is no way to have multiple aggregates with different filters on each one. You can't do annotate(fail_count=Count('jobstate').filter(state='F'), good_count=Count('jobstate').filter(state='C')) or something similar.