QUERY PLAN
Sort (cost=284.93..284.94 rows=4 width=105)
Sort Key: scpcrd.remind_date DESC
-> Nested Loop (cost=163.86..284.89 rows=4 width=105)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd (cost=0.00..12.15 rows=1 width=33)
Filter: (contract_id = 4042)
-> Group (cost=163.86..163.89 rows=4 width=525)
Group Key: sc.id, (sum("*SELECT* 1".amount))
-> Sort (cost=163.86..163.87 rows=4 width=36)
Sort Key: (sum("*SELECT* 1".amount))
-> Nested Loop Left Join (cost=0.56..163.82 rows=4 width=36)
Join Filter: ("*SELECT* 1".contract_id = sc.id)
-> Index Only Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc (cost=0.28..4.29 rows=1 width=4)
Index Cond: (id = 4042)
-> GroupAggregate (cost=0.28..159.47 rows=4 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=0.28..159.40 rows=4 width=30)
-> Subquery Scan on "*SELECT* 1" (cost=0.28..47.99 rows=1 width=10)
-> Nested Loop (cost=0.28..47.98 rows=1 width=98)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..39.67 rows=1 width=14)
Filter: (purchase_contract_id = 4042)
-> Index Scan using tbl_erp_payment_tickets_pkey on tbl_erp_payment_tickets t (cost=0.28..8.30 rows=1 width=4)
Index Cond: (id = r.erp_payment_ticket_id)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Subquery Scan on "*SELECT* 2" (cost=0.00..32.70 rows=1 width=36)
-> Nested Loop (cost=0.00..32.69 rows=1 width=124)
Join Filter: (r_1.erp_other_income_ticket_id = t_1.id)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..19.75 rows=4 width=40)
Filter: (purchase_contract_id = 4042)
-> Materialize (cost=0.00..12.77 rows=3 width=4)
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..12.75 rows=3 width=4)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Subquery Scan on "*SELECT* 3" (cost=0.00..32.56 rows=1 width=36)
-> Nested Loop (cost=0.00..32.55 rows=1 width=124)
Join Filter: (r_2.erp_cash_cost_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..19.75 rows=4 width=40)
Filter: (purchase_contract_id = 4042)
-> Materialize (cost=0.00..12.63 rows=3 width=4)
-> Seq Scan on tbl_erp_cash_cost_tickets t_2 (cost=0.00..12.61 rows=3 width=4)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Subquery Scan on "*SELECT* 4" (cost=0.28..46.14 rows=1 width=36)
-> Nested Loop (cost=0.28..46.13 rows=1 width=124)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..37.83 rows=1 width=14)
Filter: (purchase_contract_id = 4042)
-> Index Scan using tbl_erp_collect_tickets_pkey on tbl_erp_collect_tickets t_3 (cost=0.28..8.30 rows=1 width=4)
Index Cond: (id = r_3.erp_collect_ticket_id)
Filter: (status = ANY ('{2,5,1}'::integer[]))
SubPlan 1
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
SubPlan 2
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_1 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))