SELECT
p.cTrackingId AS 'Sendungsnummer',
CONCAT(CONVERT(VARCHAR, s.dDate, 104), ' ', CONVERT(VARCHAR, s.dDate, 108)) AS 'Datum',
s.cLocation AS 'Ort',
CASE
WHEN s.nStateType = 1 THEN 'Erzeugt'
WHEN s.nStateType = 2 THEN 'Startpaketzentrum'
WHEN s.nStateType = 3 THEN 'Unbekannt (3)'
WHEN s.nStateType = 4 THEN 'Zielpaketzentrum'
WHEN s.nStateType = 5 THEN 'Zustellfahrzeug'
WHEN s.nStateType = 6 THEN 'Zugestellt'
WHEN s.nStateType = 7 THEN 'Info'
WHEN s.nStateType = 8 THEN 'Unbekannt (8)'
WHEN s.nStateType = 9 THEN 'Problem'
WHEN s.nStateType = 10 THEN 'Deregistriert'
END AS 'Sendungsstatus',
s.cText AS 'Sendungsinfo'
FROM Shipping.tState s
LEFT OUTER JOIN Shipping.tPackage p on p.kPackage = s.kPackage
LEFT OUTER JOIN tVersand v on v.kVersand = p.kVersand
LEFT OUTER JOIN tLieferschein l on l.kLieferschein = v.kLieferschein
WHERE
l.kBestellung = @Key
ORDER BY p.cTrackingId, s.dDate DESC