QUERY PLAN
Limit (cost=239.68..239.69 rows=1 width=193)
-> Sort (cost=239.68..239.69 rows=1 width=193)
Sort Key: (((epop.number - COALESCE(max((sum(esop.number))), '0'::numeric)) + COALESCE(max((sum(esrp.number))), '0'::numeric))) DESC NULLS LAST, (max(eso.created_at)) DESC, epop.id DESC
-> GroupAggregate (cost=239.63..239.67 rows=1 width=193)
Group Key: epop.id
Filter: (((epop.number - COALESCE(max((sum(esop.number))), '0'::numeric)) + COALESCE(max((sum(esrp.number))), '0'::numeric)) > '0'::numeric)
-> Sort (cost=239.63..239.64 rows=1 width=161)
Sort Key: epop.id DESC
-> Nested Loop Left Join (cost=229.39..239.62 rows=1 width=161)
Join Filter: (esop_1.erp_sales_order_product_id = epop.id)
-> Hash Right Join (cost=212.35..222.41 rows=1 width=129)
Hash Cond: (esop.erp_sales_order_product_id = epop.id)
-> HashAggregate (cost=22.41..27.20 rows=383 width=36)
Group Key: esop.erp_sales_order_product_id
-> Hash Join (cost=9.96..20.35 rows=412 width=9)
Hash Cond: (esop.erp_sales_outbound_id = esob.id)
-> Seq Scan on tbl_erp_sales_outbound_products esop (cost=0.00..9.25 rows=425 width=13)
-> Hash (cost=7.64..7.64 rows=186 width=4)
-> Seq Scan on tbl_erp_sales_outbounds esob (cost=0.00..7.64 rows=186 width=4)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> Hash (cost=189.92..189.92 rows=1 width=97)
-> Nested Loop Left Join (cost=144.05..189.92 rows=1 width=97)
Filter: ((client.sales_rep = 1001) OR (eso.created_by = 1001) OR (rvr.user_id = 1001) OR (eso.created_by = ANY ('{1000,1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1011,1012,1013,1014,1015,1016,1017,1018,1019,1020,1021,1022,1023,1025,1026,1027,1028,1029,1030,5015,5016,5022,5023,5024,5025,5026,5027,5028,5029,5030,5031,5032,5033,5037,5043,5044,5045,5047,5048,5049,5050,5062,5063,5064,5065,5066,5069,5070,5071,5072,5073,5074,5075,5076,5078,5079,5080,5081,5082,5083,5085,5086,5087,5088,5089,5093,5094,5095,5096,5101,5102,5104,5105,5123,5124,5125,5126,5128,5135,5203,5208,5209,5229,5230,5247,5248,5249,5250,5252,5258,5259,5293,5294,5295,5296,5297,5298,5299,5300,5301,5302,5303,5304,5305,5309,5311,5329,5332,5333,5351,5356,5357,5358,5359,5360,5364,5388,5393,5404,5409,5411,5412,5413,5419,5420,5440,5445,5446,5448,5449,5461,5467,5476,5497,5499,5500}'::integer[])))
-> Nested Loop Left Join (cost=143.78..189.08 rows=1 width=109)
Join Filter: (rvr.erp_sales_order_id = eso.id)
-> Hash Right Join (cost=143.78..184.81 rows=1 width=109)
Hash Cond: (purchase_rel.project_id = eso.project_id)
-> Seq Scan on tbl_purchase_client_rels purchase_rel (cost=0.00..33.11 rows=2111 width=8)
-> Hash (cost=143.76..143.76 rows=1 width=113)
-> Nested Loop (cost=83.12..143.76 rows=1 width=113)
-> Hash Join (cost=82.84..143.39 rows=1 width=89)
Hash Cond: (epop.product_id = product.id)
-> Seq Scan on tbl_erp_sales_order_products epop (cost=0.00..54.71 rows=2217 width=89)
Filter: (number > '0'::numeric)
-> Hash (cost=82.83..82.83 rows=1 width=4)
-> Seq Scan on tbl_products product (cost=0.00..82.83 rows=1 width=4)
Filter: ((product_brand = 4) AND (product_serie = 88))
-> Index Scan using tbl_erp_sales_orders_pkey on tbl_erp_sales_orders eso (cost=0.28..0.38 rows=1 width=24)
Index Cond: (id = epop.erp_sales_order_id)
Filter: ((NOT is_done) AND (status = 2))
-> Seq Scan on tbl_erp_sales_order_verify_rels rvr (cost=0.00..2.45 rows=145 width=8)
-> Index Scan using tbl_clients_pkey on tbl_clients client (cost=0.28..0.64 rows=1 width=8)
Index Cond: (id = eso.client_id)
-> GroupAggregate (cost=17.04..17.12 rows=4 width=36)
Group Key: esop_1.erp_sales_order_product_id
-> Sort (cost=17.04..17.05 rows=4 width=9)
Sort Key: esop_1.erp_sales_order_product_id
-> Nested Loop (cost=2.68..17.00 rows=4 width=9)
-> Nested Loop (cost=2.53..16.07 rows=4 width=13)
-> Hash Join (cost=2.26..14.64 rows=4 width=9)
Hash Cond: (esosp.id = esrp.erp_sales_outbound_spent_product_id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products esosp (cost=0.00..10.34 rows=534 width=8)
-> Hash (cost=2.21..2.21 rows=4 width=9)
-> Hash Join (cost=1.12..2.21 rows=4 width=9)
Hash Cond: (esrp.erp_sales_return_id = esr.id)
-> Seq Scan on tbl_erp_sales_return_products esrp (cost=0.00..1.06 rows=6 width=13)
-> Hash (cost=1.07..1.07 rows=4 width=4)
-> Seq Scan on tbl_erp_sales_returns esr (cost=0.00..1.07 rows=4 width=4)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Index Scan using tbl_erp_sales_outbound_products_pkey on tbl_erp_sales_outbound_products esop_1 (cost=0.27..0.36 rows=1 width=12)
Index Cond: (id = esosp.erp_sales_outbound_product_id)
-> Index Scan using tbl_erp_sales_outbounds_pkey on tbl_erp_sales_outbounds esob_1 (cost=0.14..0.23 rows=1 width=4)
Index Cond: (id = esop_1.erp_sales_outbound_id)
Filter: (status = ANY ('{1,2,5}'::integer[]))