I have 2 tables:
Table class
:
id serial4 PRIMARY KEY
name varchar(64)
code varchar(64)
Table class_event
, where I store events related to classes, such as "started" and "ended".
id serial4
class_id int4 NOT NULL // -> FK to the class table
event_type varchar(1) NOT NULL // -> 's' for started, 'e' for ended.
I need a query the amount of times each class has been started and ended. This works:
select
c.code,
c.name,
count(started.id) "started"
from "class" c
left join (select id, class_id, event_type from "class_event" where event_type = 's') started
on started.klass_id = c.id
group by c.code, c.name
order by started desc;
But when I do exactly the same to get the amount of ended classes it shows incorrect amounts:
select
c.code,
c.name,
count(started.id) "started",
count(ended.id) "ended"
from "class" c
left join (select id, class_id, event_type from "class_event" where event_type = 's') started
on started.klass_id = c.id
left join (select id, class_id, event_type from "class_event" where event_type = 'e') ended
on ended.klass_id = c.id
group by c.code, c.name
order by started desc;
Also, the query takes significantly more time to execute. Is there anything I'm missing?