where l.market_id = ldsi.market_id and l.material_id = ldsi.material_id and l.pid is null and ldsi.incoming_batch = l.incoming_batch )
where l.is_deleted =0 AND l.market_id = ldsi.market_id and l.material_id = ldsi.material_id and l.pid is null and ldsi.incoming_batch = l.incoming_batch )
ELSE (select sum(l.quantity_stock)
from logpm_distribution_stock_list l
where l.market_id = ldsi.market_id and l.material_id = ldsi.material_id and l.pid = ldsi.pid and l.pid is not null and ldsi.incoming_batch = l.incoming_batch )
where l.is_deleted =0 AND l.market_id = ldsi.market_id and l.material_id = ldsi.material_id and l.pid = ldsi.pid and l.pid is not null and ldsi.incoming_batch = l.incoming_batch )