QUERY PLAN
Unique (cost=1108.61..1108.68 rows=1 width=540)
-> Sort (cost=1108.61..1108.61 rows=1 width=540)
Sort Key: s.name, s.abbr, s.pinyin_shortcut, s.main_products, s.company_nature, s.website, s.established_at, s.capital, s.corp_rep, s.reg_address, s.telephone, s.fax, s.description, s.main_contact, s.created_by, s.created_at, s.updated_at, s.working_address, s.capital_currency, s.payment_cycle, s.account_number, (sum((sum(pc.amount)))), (sum((sum(CASE WHEN (pcp.collected IS NOT NULL) THEN pcp.collected ELSE '0'::numeric END)))), ((sum((sum(pc.amount))) - sum((sum(CASE WHEN (pcp.collected IS NOT NULL) THEN pcp.collected ELSE '0'::numeric END))))), (sum((sum(CASE WHEN ((sum(tbl_purchase_contract_invoices.amount)) IS NOT NULL) THEN (sum(tbl_purchase_contract_invoices.amount)) ELSE '0'::numeric END)))), ((sum((sum(pc.amount))) - sum((sum(CASE WHEN ((sum(tbl_purchase_contract_invoices.amount)) IS NOT NULL) THEN (sum(tbl_purchase_contract_invoices.amount)) ELSE '0'::numeric END)))))
-> GroupAggregate (cost=1045.18..1108.60 rows=1 width=540)
Group Key: s.id
-> Nested Loop Left Join (cost=1045.18..1108.24 rows=16 width=476)
Join Filter: (pc.supplier_id = s.id)
-> Index Scan using tbl_suppliers_pkey on tbl_suppliers s (cost=0.28..8.29 rows=1 width=380)
Index Cond: (id = 431)
-> GroupAggregate (cost=1044.90..1099.77 rows=16 width=100)
Group Key: pc.supplier_id
-> Hash Left Join (cost=1044.90..1099.33 rows=16 width=74)
Hash Cond: (pc.id = pcp.contract_id)
-> Hash Right Join (cost=197.14..251.53 rows=16 width=46)
Hash Cond: (tbl_purchase_contract_invoices.contract_id = pc.id)
-> HashAggregate (cost=73.55..100.59 rows=2164 width=36)
Group Key: tbl_purchase_contract_invoices.contract_id
-> Seq Scan on tbl_purchase_contract_invoices (cost=0.00..60.70 rows=2569 width=10)
Filter: ((red_invoice_id IS NOT NULL) OR (NOT is_cancel))
-> Hash (cost=123.40..123.40 rows=16 width=14)
-> Seq Scan on tbl_purchase_contracts pc (cost=0.00..123.40 rows=16 width=14)
Filter: ((NOT is_chargeback) AND (NOT is_draft) AND (NOT is_refuse) AND (supplier_id = 431))
-> Hash (cost=845.26..845.26 rows=200 width=36)
-> Subquery Scan on pcp (cost=840.76..845.26 rows=200 width=36)
-> HashAggregate (cost=840.76..843.26 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=132.28..770.45 rows=4687 width=124)
-> Subquery Scan on "*SELECT* 1" (cost=132.28..229.02 rows=2458 width=90)
-> Hash Join (cost=132.28..198.30 rows=2458 width=86)
Hash Cond: (r.erp_payment_ticket_id = t.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..41.01 rows=2501 width=18)
-> Hash (cost=103.39..103.39 rows=2311 width=44)
-> Seq Scan on tbl_erp_payment_tickets t (cost=0.00..103.39 rows=2311 width=44)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash Join (cost=1.18..3.98 rows=8 width=126)
Hash Cond: (t_1.id = r_1.erp_other_income_ticket_id)
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..2.50 rows=38 width=50)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=1.08..1.08 rows=8 width=44)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.08 rows=8 width=44)
-> Nested Loop (cost=0.29..323.63 rows=45 width=122)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.45 rows=45 width=44)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_2 (cost=0.29..7.15 rows=1 width=46)
Index Cond: (id = r_2.erp_cash_cost_ticket_id)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash Join (cost=122.76..191.53 rows=2176 width=109)
Hash Cond: (r_3.erp_collect_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..35.83 rows=2183 width=18)
-> Hash (cost=95.42..95.42 rows=2187 width=33)
-> Seq Scan on tbl_erp_collect_tickets t_3 (cost=0.00..95.42 rows=2187 width=33)
Filter: (status = ANY ('{2,5}'::integer[]))