Here's the resulting request, it is too complex, the audits join and where clause can be simpler, here's the resulting query:
select i.id, i.uuid, i.updated_at, i.created_at, i.responses, i.validated_data, audits.audits, i.is_valid, i.is_completed, i.is_validated, i.is_questionable, i.survey_id, participant.username, case when participant.facebook_id is null then false else true end facebook, case when participant.google_id is null then false else true end google
from sv_interviews as i
left join sv_participants as participant on i.participant_id = participant.id
left join (select interview_id, json_agg(json_build_object(error_code, count)) as audits from (select interview_id, error_code, count(*) from sv_audits group by interview_id, error_code order by count asc) as audits_cnt group by interview_id) as audits on i.id = audits.interview_id
where i.is_active IS TRUE
AND participant.is_valid IS TRUE
AND participant.is_test IS NOT TRUE
AND i.is_valid IS TRUE
AND i.responses->>'_isCompleted' = 'true'
AND i.id in (select distinct interview_id from sv_audits where error_code = 'T_L_transitTripNoMatch') order by responses->>'accessCode' asc
Here's the resulting request, it is too complex, the audits join and where clause can be simpler, here's the resulting query: