"SELECT ps.*, ss.integration_id as source_integration_id, ss.remote_id as source_remote_id, ss.payload as source_payload, ss.is_deactivated as source_is_deactivated, events.event_ids FROM %s ps LEFT OUTER JOIN %s ss on (ps.id = ss.profile_id) LEFT OUTER JOIN (SELECT att.profile_id, att.status, array_agg(att.event_id) as event_ids FROM %s att WHERE att.status IN(?, ?) AND att.profile_id = ? AND att.apikey = ? GROUP BY att.profile_id, att.status) events WHERE ps.apikey = ? AND ps.id = ?" (db/route tables/profile-tbl apikey) (db/route tables/profile-source-tbl apikey) (db/route tables/attendee-tbl apikey)) [(tables/attendee-status-str"->int "joined") (tables/attendee-status-str"->int "prejoined") id apikey apikey id]] {:raw-results? true}) (either/fmap -format-profile-with-sources))) Component Query, not DSL Either Partitioning