left outer join with count

From: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
To: pgsql-sql(at)postgresql(dot)org
Subject: left outer join with count
Date: 2020-05-29 16:09:16
Message-ID: 202005291709.16647.gary.stainburn@ringways.co.uk
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have the following select:

select sj.*, sr.*, wd.doc_count from
service_jobs sj
left outer join service_receptions sr on sr.sr_id = sj.sj_sr_id
left outer join (select sj_id, count(sj_id) as doc_count from work_documents wd group by sj_id) wd on wd.wd_sj_id = sj.sj_id;

While the select works, I can't help thinking that the last join is expensive. Is there a cleaner (quicker) method of doing this?

Gary

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gábor SZŰCS 2020-05-30 04:43:06 Re: left outer join with count
Previous Message Igor Shmukler 2020-05-20 15:10:17 Re: Postgres stored procedure errs while parsing JSONB object