Hi,
Try this:
SELECT DISTINCT
DATEDIFF(DD, T0.DocDueDate, T3.DocDueDate) AS 'Lapsed Days',
CASE
WHEN DATEDIFF(DD, T0.DocDueDate, T3.DocDueDate) >= 5 THEN 'Shipped'
WHEN DATEDIFF(DD, T0.DocDueDate, T3.DocDueDate) BETWEEN 1 AND 4 THEN 'Shipped'
WHEN DATEDIFF(DD, T0.DocDueDate, T3.DocDueDate) = 0 THEN 'Shipped'
WHEN DATEDIFF(DD, T0.DocDueDate, T3.DocDueDate) < 0 THEN 'EarlyShipment'
ELSE 'Not Shipped'
END AS 'Ship Status',
T0.DocNum AS 'Sales Ord',
T0.DocDueDate AS 'Exp Del Date',
T3.DocNum AS 'Del Num',
T3.DocDueDate AS 'Del Date',
T5.DocNum AS 'Inv Num' FROM ORDR T0 left outer JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry left outer join DLN1 T2 on T2.[BaseEntry] = t0.docentry and T2.[BaseLine] = T1.[LineNum] left outer JOIN ODLN T3 ON T2.DocEntry = T3.DocEntry left join INV1 T4 on T4.[BaseEntry] = t3.docentry and T4.[BaseLine] = T2.[LineNum] left outer JOIN OINV T5 ON T4.DocEntry = T5.DocEntry WHERE T0.[DocType] = 'I' ORDER BY DATEDIFF(DD, T0.DocDueDate, T3.DocDueDate),
T0.DocDueDate DESC,
T0.DocNum,
T3.DocDueDate,
T3.DocNum,
T5.DocNum
The above query will show open sales order and closed sales order by delivery as well as by invoice.
Thanks & Regards,
Nagarajan