I would like to count the waves
available (LEFT OUT JOIN
). My problem is that I also have another LEFT OUTER JOIN
on invitations
.
The idea is to have all the stats from the same SQL request. Stats from invitations
and stats from waves
.
An event has many waves
An event has many invitations
CREATE TABLE public.events (
id bigint NOT NULL,
uuid uuid DEFAULT public.gen_random_uuid() NOT NULL
name character varying NOT NULL
);
CREATE TABLE public.invitations (
id bigint NOT NULL,
uuid uuid DEFAULT public.gen_random_uuid() NOT NULL,
event_id bigint NOT NULL
);
CREATE TABLE public.invitation_waves (
id bigint NOT NULL,
uuid uuid DEFAULT public.gen_random_uuid() NOT NULL,
name character varying NOT NULL,
wavable_type character varying,
wavable_id bigint,
scheduled_at timestamp(6) without time zone
);
I have tested 2 options
Option 1
SELECT CAST(sum((case when (waves.id IS NOT NULL) then 1 else 0 end)) AS INTEGER) as total_waves
FROM "events"
LEFT OUTER JOIN "waves" ON "waves"."wavable_type" = 'Event' AND
"waves"."wavable_id" = "events"."id"
LEFT OUTER JOIN "invitations" ON "invitations"."event_id" = "events"."id"
WHERE "events"."uuid" = 'XXX'
GROUP BY "events"."id"
-> It gives me the number of invitations created if above the number of waves
Option 2 (with DISTINCT
)
SELECT CAST(sum(DISTINCT(case when (waves.id IS NOT NULL) then 1 else 0 end)) AS INTEGER) as total_waves
FROM "events"
LEFT OUTER JOIN "waves" ON "waves"."wavable_type" = 'Event' AND
"waves"."wavable_id" = "events"."id"
LEFT OUTER JOIN "invitations" ON "invitations"."event_id" = "events"."id"
WHERE "events"."uuid" = 'XXX'
GROUP BY "events"."id"
-> Always returns me 1
which is, I suppose, the number of events matching the request (I’ve scoped it with the uuid).
Not having the DISTINCT
is actually multiplying the correct sum by the number of waves.
Having the DISTINCT
restrict the entire sum to the number of waves.
Option 3 (sub select) ✅
I’m currently trying this option based on this answer.
It is working but I’m concerned about the dependency between GROUP BY
and the selected columns from the LEFT OUTER JOIN
.
SELECT CAST(sum((case
when (invitations.status = 4 or invitations.status = 5 or invitations.status = 6) then 1
else 0 end)) AS INTEGER) as total_validated,
total_waves,
waves_scheduled
FROM "events"
LEFT OUTER JOIN "invitations" ON "invitations"."event_id" = "events"."id"
LEFT OUTER JOIN (SELECT distinct on (wavable_id)
wavable_id as event_id,
CAST(sum((case when (invitation_waves.id IS NOT NULL) then 1 else 0 end)) AS INTEGER) as total_waves,
CAST(sum((case when (invitation_waves.scheduled_at IS NOT NULL) then 1 else 0 end)) AS INTEGER) as waves_scheduled
FROM invitation_waves
WHERE wavable_type = 'Event'
GROUP BY 1
ORDER BY wavable_id) as "wave_stats" on "wave_stats"."event_id" = "events"."id"
WHERE "events"."uuid" = '1ee5ec72-6f3c-404c-871c-5c5724f6a1ed'
GROUP BY "events"."id", total_waves, waves_scheduled
2
Answers
It is not quite clear what you realy want (without sample data and expected result), but it looks like you want to get number of invitations and/or number of waves and/or total number of both – per event. If that is so, using your table strutures and my blind guessed data, the code below could be something that might help you get what you need:
… and if you want just grand totals for all events then just exclude e.id and e.name from selection list and don’t use Group By and Order By:
See the fiddle here.
NOTES:
The following SQL creates the tables and populates them with test values to verify that the query is producing the intended results. The event names are the expected counts for invitations, validated invitations, waves, and scheduled waves.
The following demonstrates using
COUNT(DISTINCT ...) FILTER (WHERE ...)
to report counts for multiple conditions within a single query. WithoutDISTINCT
, the query would report incorrect counts for events that have both invitations and waves when either has more than one associated with the event.Running the query with the test data produces the following results: