QUERY PLAN
Aggregate (cost=14.87..14.88 rows=1 width=96)
-> Merge Join (cost=10.14..14.86 rows=1 width=72)
Merge Cond: (tbl_erp_inventory_product_spents.erp_inventory_product_id = e.id)
-> GroupAggregate (cost=5.79..9.67 rows=66 width=76)
Group Key: tbl_erp_inventory_product_spents.erp_inventory_product_id
-> Sort (cost=5.79..5.96 rows=68 width=29)
Sort Key: tbl_erp_inventory_product_spents.erp_inventory_product_id
-> Seq Scan on tbl_erp_inventory_product_spents (cost=0.00..3.72 rows=68 width=29)
Filter: (CASE WHEN (erp_inventory_build_spent_id IS NOT NULL) THEN ('1'::text || ((erp_inventory_build_spent_id)::character varying)::text) WHEN (erp_sales_outbound_id IS NOT NULL) THEN ('2'::text || ((erp_sales_outbound_id)::character varying)::text) WHEN (erp_sales_gift_id IS NOT NULL) THEN ('3'::text || ((erp_sales_gift_id)::character varying)::text) WHEN (erp_inventory_lost_id IS NOT NULL) THEN ('4'::text || ((erp_inventory_lost_id)::character varying)::text) ELSE NULL::text END IS NOT NULL)
-> Sort (cost=4.35..4.35 rows=1 width=4)
Sort Key: e.id
-> Seq Scan on tbl_erp_inventory_products e (cost=0.00..4.34 rows=1 width=4)
Filter: (product_id = 1812)