QUERY PLAN
Aggregate (cost=347.34..347.35 rows=1 width=32)
-> HashAggregate (cost=343.49..345.20 rows=171 width=70)
Group Key: sc.id, tbl_purchase_contracts.erp_settle_amount
-> Hash Right Join (cost=286.87..342.64 rows=171 width=38)
Hash Cond: (tbl_purchase_contracts.id = sc.id2)
-> HashAggregate (cost=76.45..99.21 rows=2276 width=10)
Group Key: tbl_purchase_contracts.id
-> Seq Scan on tbl_purchase_contracts (cost=0.00..70.76 rows=2276 width=10)
-> Hash (cost=210.24..210.24 rows=15 width=36)
-> Subquery Scan on sc (cost=210.05..210.24 rows=15 width=36)
-> Sort (cost=210.05..210.09 rows=15 width=663)
Sort Key: (CASE WHEN (t0.id IS NOT NULL) THEN t0.remind_date ELSE sc_1.signed_at END) DESC
-> Hash Right Join (cost=190.68..209.75 rows=15 width=663)
Hash Cond: (t0.contract_id = sc_1.id)
Filter: (((t0.id IS NOT NULL) AND (t0.paid_amount <> '0'::numeric)) OR ((t0.id IS NULL) AND (sc_1.erp_settle_amount > '0'::numeric)))
-> HashAggregate (cost=119.73..128.08 rows=835 width=67)
Group Key: t0.id
-> Hash Join (cost=99.02..117.64 rows=835 width=18)
Hash Cond: (t0.contract_id = t1.id)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates t0 (cost=0.00..16.41 rows=841 width=18)
-> Hash (cost=70.76..70.76 rows=2261 width=4)
-> Seq Scan on tbl_purchase_contracts t1 (cost=0.00..70.76 rows=2261 width=4)
Filter: (NOT is_chargeback)
-> Hash (cost=70.76..70.76 rows=15 width=18)
-> Seq Scan on tbl_purchase_contracts sc_1 (cost=0.00..70.76 rows=15 width=18)
Filter: ((NOT is_draft) AND (NOT is_refuse) AND is_chargeback)