SELECT DISTINCT client real_client_id FROM client LEFT JOIN real_clien

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
SELECT DISTINCT "client"."real_client_id"
FROM "client"
LEFT JOIN "real_client" "rc_uniq" ON client.real_client_id = rc_uniq.id
LEFT JOIN "tkt"."client" "tkt_client" ON client.ticket_client_id = tkt_client.id
LEFT JOIN "tkt"."ticket" "t3" ON t3.client_id = tkt_client.id AND
t3.datetime BETWEEN COALESCE('2018-08-01', '1800-05-08' :: DATE) AND COALESCE('2019-02-04', '3016-05-08' :: DATE) AND
t3.ticket_type = 1
LEFT JOIN "tkt"."ticket" "t4" ON t4.client_id = tkt_client.id AND
t4.datetime BETWEEN COALESCE('2018-09-01', '1800-05-08' :: DATE) AND COALESCE('2019-02-04', '3016-05-08' :: DATE) AND
t4.ticket_type = 1
WHERE ("client"."instance_id" = 5)
AND ("rc_uniq"."instance_id" = 5)
AND ("client"."real_client_id" IS NOT NULL)
GROUP BY "client"."real_client_id"
HAVING (count(t3.id) BETWEEN coalesce((1), 1) AND coalesce((5), 999999))