QUERY PLAN
Aggregate (cost=112.13..112.14 rows=1 width=96)
-> Merge Join (cost=72.32..112.12 rows=1 width=72)
Merge Cond: (tbl_erp_inventory_product_spents.erp_inventory_product_id = e.id)
-> GroupAggregate (cost=58.10..92.25 rows=450 width=76)
Group Key: tbl_erp_inventory_product_spents.erp_inventory_product_id
-> Sort (cost=58.10..59.47 rows=548 width=32)
Sort Key: tbl_erp_inventory_product_spents.erp_inventory_product_id
-> Seq Scan on tbl_erp_inventory_product_spents (cost=0.00..33.17 rows=548 width=32)
Filter: (CASE WHEN (erp_inventory_build_spent_id IS NOT NULL) THEN ('1'::text || ((erp_inventory_build_spent_id)::character varying)::text) WHEN (erp_inventory_disassemble_spent_id IS NOT NULL) THEN ('5'::text || ((erp_inventory_disassemble_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=14.22..14.23 rows=1 width=4)
Sort Key: e.id
-> Seq Scan on tbl_erp_inventory_products e (cost=0.00..14.21 rows=1 width=4)
Filter: (product_id = 2080)