This view is used to lookup lineshipped and linescomplete and the underlying table shipments has an Index on Custnbr,ordernbr,ordernbrline with an include of shipdate. Look for help on how to re-write without multiple
subselects maybe use CTE to do the join and make process quicker. The Shipment tables holds 200,000+ records.
Usage:
Select * from vw
where lineontime = 1
select custNbr, ordernbr, ordernbrline, shipsite, Partnbr,
convert(char(10), shipdate, 120) as ShipDate, convert(char(10), Pdate, 120) as Pdate, LineOnTime = case (select count(*) from shipments s2 where shipments.custnbr = s2.custnbr and s2.ordernbr = shipments.ordernbr and s2.ordernbrline = shipments.ordernbrline and s2.shippedontime = 0) when 0 then 1 else 0 end, LineComplete = case (select count(*) from shipments s3 where shipments.custnbr = s3.custnbr and s3.ordernbr = shipments.ordernbr and s3.ordernbrline = shipments.ordernbrline and s3.shippedcomplete = 0 and not exists ( select s4.ordernbr from shipments s4 where s4.custnbr = s3.custnbr and s4.ordernbr = s3.ordernbr and s4.ordernbrline = s3.ordernbrline and datediff(dd, s3.shipdate, s4.shipdate) = 0 and s4.shippedcomplete = 1)) when 0 then 1 else 0 end from shipments where substring(prodline,1,1) = '2'Thanks.