QUERY PLAN
Aggregate (cost=20.23..20.24 rows=1 width=8)
-> Sort (cost=19.99..20.03 rows=16 width=546)
Sort Key: sub.wrote_date DESC
-> Result (cost=2.05..19.67 rows=16 width=546)
-> Append (cost=2.05..19.51 rows=16 width=4)
-> Subquery Scan on sub (cost=2.05..2.07 rows=1 width=4)
-> GroupAggregate (cost=2.05..2.06 rows=1 width=546)
Group Key: r.id
-> Sort (cost=2.05..2.05 rows=1 width=8)
Sort Key: r.id
-> Nested Loop (cost=0.00..2.04 rows=1 width=8)
Join Filter: (r.id = rp.erp_sales_return_id)
-> Seq Scan on tbl_erp_sales_returns r (cost=0.00..1.01 rows=1 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Seq Scan on tbl_erp_sales_return_products rp (cost=0.00..1.01 rows=1 width=8)
Filter: (product_id = 2177)
-> Subquery Scan on "*SELECT* 2" (cost=17.13..17.43 rows=15 width=4)
-> HashAggregate (cost=17.13..17.28 rows=15 width=546)
Group Key: r_1.id
-> Hash Join (cost=2.71..17.07 rows=24 width=8)
Hash Cond: (rp_1.erp_purchase_gift_id = r_1.id)
-> Hash Right Join (cost=1.27..15.56 rows=24 width=4)
Hash Cond: (eip.erp_purchase_gift_product_id = rp_1.id)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..12.97 rows=497 width=8)
-> Hash (cost=1.26..1.26 rows=1 width=8)
-> Seq Scan on tbl_erp_purchase_gift_products rp_1 (cost=0.00..1.26 rows=1 width=8)
Filter: (product_id = 2177)
-> Hash (cost=1.25..1.25 rows=15 width=8)
-> Seq Scan on tbl_erp_purchase_gifts r_1 (cost=0.00..1.25 rows=15 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))