QUERY PLAN
Aggregate (cost=49.01..49.02 rows=1 width=96)
-> Nested Loop (cost=33.47..48.99 rows=1 width=72)
Join Filter: (tbl_erp_inventory_product_spents.erp_inventory_product_id = e.id)
-> Seq Scan on tbl_erp_inventory_products e (cost=0.00..14.21 rows=1 width=4)
Filter: (product_id = 2080)
-> GroupAggregate (cost=33.47..34.44 rows=15 width=76)
Group Key: tbl_erp_inventory_product_spents.erp_inventory_product_id
-> Sort (cost=33.47..33.50 rows=15 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=15 width=32)
Filter: ((erp_inventory_lost_id IS NOT NULL) AND (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))