Tried doing this a few times and not getting success. I have 2 tables orders and shipments. Order number is unique in the orders table but the value in the shipments table references order.number which can occur more than 1 time as an order can have multiple shipments.
So table orders has order.number and shipments has shipments.number, shipments.order_number and shipments.stock_location
I would like to only return the order.number where no shipments within that order.number were no shipments within that order shipped by a stock location. If I apply a where statement it just is removing the lines in the data tied to the shipment.number not taking into consideration a order.number could have a shipment where shipment.stock_location did ship from my excluded warehouse.
If it makes more sense here is actually my full code which is not working. What Im attempting to do is create a mailing list for any order where it fully drop shipped on all its shipments.
select orders.number, addresses.firstname,
addresses.lastname, addresses.address1, addresses.address2,
addresses.city, states.abbr,
addresses.zipcode
from orders
join addresses on orders.ship_address_id = addresses.id
join shipments on shipments.order_id = orders.id
join states on states.id = addresses.state_id
where orders.id NOT IN (
select shipments.order_id from shipments
where shipments.stock_location_id !=1 and orders.shipment_state='shipped')
AND orders.completed_at>= {{daterangepicker1.startFormattedString}} and
orders.completed_at<= {{daterangepicker1.endFormattedString}}
group by orders.number
order by orders.completed_at DESC;
I do not want any order numbers to show if any shipments.number within that order number shipped via stock location 1