sql use bo -- (LADEN) Gibt eine Liste aller Kunden eines Liefertages in Tourreihenfolge aus Drop Table tmpTour Create Table tmpTour (Bezeichnung varchar(50), BestellID int, _ Vorname varchar(50), Nachname Varchar(50), Strasse varchar(50), PLZ varchar(10),_ Ort varchar(50), AdressId varchar(12), Tourpos int, TourNr int) --alle Kunden eines Liefertags Insert into tmpTour (Bezeichnung, BestellId, _ Vorname, Nachname, Strasse, Plz, Ort, AdressId, Tourpos, TourNr) _ (Select b.Bezeichnung, b.BestellId, _ a.Vorname, a.Nachname, a.Strasse, a.Plz, a.Ort, a.AdressId, t.Tourpos, t.TourNr _ From dTour t join dBestellung b _ On t.TourNr = b.TourNr Join dTourbeschreibung tb On t.TourNr = tb.TourNr _ Join Adresse a On a.AdressId = b.AdressId _ Where b.TourNr = tb.TourNr And tb.Liefertag = {Liefertag} _ And t.BestellId = b.BestellId And '{Montag}' between b.von and coalesce(b.bis,'{Montag}') _ And b.LadenId = 'LADEN' And Exists _ (Select Top 1 c.EAN From dBestellPos c _ Where c.BestellId = b.BestellId _ And datediff(dd, c.von, '{Montag}') >= 0 _ And ( c.bis is Null Or c.bis = 0 Or datediff(dd, c.bis, '{Montag}') <= 0) _ And (datediff(dd, '{Montag}' , c.von)/7) % c.Periode = 0)) --Alle Kunden mit Pause oder Lieferadresse werden gelöscht Delete t from tmpTour t Join _ (Select BestellId From dLiBesonderheit _ Where '{Montag}' Between von And coalesce(bis,'{Montag}')) b On t.Bestellid = b.Bestellid Insert into tmpTour (Bezeichnung, BestellId, Tourpos, Vorname, Nachname, Strasse, Plz, Ort, _ AdressId, TourNr) _ Select b.Bezeichnung, b.BestellId, t.Tourpos, lb.Vorname, lb.Nachname, lb.Strasse, lb.Plz, lb.Ort, _ a.AdressId, lb.TourNr _ From dBestellung b join dLiBesonderheit lb _ On b.BestellId = lb.BestellId Join dTourbeschreibung tb On tb.tournr = lb.Tournr _ Join Adresse a on b.AdressId = a.AdressId Join dTour t On t.TourNr = lb.Tournr _ Where lb.TourNr = tb.TourNr And tb.Liefertag = {Liefertag} _ And b.LadenId = 'LADEN' And coalesce(lb.PLZ,'x') <> 'Pause' And t.uRowid = lb.Rowid And '{Montag}' Between lb.von And coalesce(lb.bis, '{Montag}') _ And Exists _ (Select Top 1 c.EAN From dBestellPos c _ Where c.BestellId = b.BestellId _ And datediff(dd, c.von, '{Montag}') >= 0 _ And ( c.bis is Null Or c.bis = 0 Or datediff(dd, c.bis, '{Montag}') <= 0) _ And (datediff(dd, '{Montag}' , c.von)/7) % c.Periode = 0) Select TourNr, count(TourNr) as Anz From tmptour Group by TourNr order by TourNr