QUERY PLAN
Nested Loop (cost=3.78..8.52 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..4.34 rows=1 width=4)
Filter: (product_id = 1624)
-> GroupAggregate (cost=3.78..4.07 rows=5 width=76)
Group Key: tbl_erp_inventory_product_spents.erp_inventory_product_id
-> Sort (cost=3.78..3.79 rows=5 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=5 width=29)
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_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))