redesign view maybe use cte rather than subquery

  

  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.
August 26th, 2015 7:55pm

You could try changing to

from shipments
  left outer join (select custnbr, ordernbr, ordernbrline, count(*) from shipments where shippedontime = 0 group by custnbr, ordernbr, ordernbrline) s2
  on shipments.custnbr = s2.custnbr and s2.ordernbr = shipments.ordernbr and s2.ordernbrline = shipments.ordernbrline

etc

No idea how much of a difference it would make though

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 8:36pm

 Thanks.

   Can you show full code.

August 26th, 2015 9:55pm

Can you tell if the combination of

custNbr, OrderNbr, OrderNbrLine is unique in the Shipments table or there may be multiple same rows?

Why LineOnTime = 1 if there is at least one ShippedOnTime? If the above combination of 3 columns is always unique, then you don't need to do a separate sub-select. If it is not unique, then why LineOnTime is 1 when one of the items is On Time? What if one is On Time but another is Delayed, would this mean it is still LineOnTime = 1? 

Also, instead of using substring(prodLine,1,1) = '2' it may be better to use prodline lik

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 12:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics