Reputation: 101811
Given the tables projects
:
id | bigint | not null default nextval('projects_id_seq'::regclass)
name | character varying |
created_at | timestamp(6) without time zone | not null
updated_at | timestamp(6) without time zone | not null
and tasks
:
id | bigint | not null default nextval('tasks_id_seq'::regclass)
name | character varying |
project_id | bigint | not null
created_at | timestamp(6) without time zone | not null
updated_at | timestamp(6) without time zone | not null
status | task_status |
task_status
is an enum:
CREATE TYPE task_status AS ENUM ('pending', 'in_progress', 'complete')
I want to select name and id from projects and counts of the pending
, in_progress
and complete
tasks.
id | name | pending_tasks_count | in_progress_tasks_count | complete_tasks_count
----+--------------------------------+---------------------+-------------------------+----------------------
2 | Dickens, Walker and Rutherford | 1 | 8 | 5
5 | Bailey-Kreiger | 0 | 0 | 4
4 | Ledner, Ullrich and Davis | 2 | 1 | 2
1 | Price-Fisher | 3 | 4 | 1
3 | Harber LLC | 1 | 2 | 1
What I have so far is just doing three ungainly subqueries:
SELECT projects.id, projects.name,
(SELECT COUNT(tasks.*) FROM tasks WHERE tasks.project_id = projects.id
AND tasks.status = 'pending') AS pending_tasks_count,
(SELECT COUNT(tasks.*) FROM tasks WHERE tasks.project_id = projects.id
AND tasks.status = 'in_progress') AS in_progress_tasks_count,
(SELECT COUNT(tasks.*) FROM tasks WHERE tasks.project_id = projects.id
AND tasks.status = 'complete') AS complete_tasks_count
FROM projects
LEFT OUTER JOIN tasks
ON tasks.project_id = projects.id GROUP BY projects.id
Is there a more elegant/performant solution to select the aggregates?
Upvotes: 1
Views: 42
Reputation: 222402
You can pivot your data with conditional aggregation. In Postgres, the filter
functionality of aggregate functions comes handy for this:
select
p.id,
p.name,
count(*) filter(where t.status = 'pending') pending_tasks_count,
count(*) filter(where t.status = 'in_progress') in_progress_tasks_count,
count(*) filter(where t.status = 'complete') complete_tasks_count
from projects p
inner join tasks t on t.project_id = p.id
group by p.id, p.name
Upvotes: 2