Dear Pratik,
Firstly thanks for grading my comment. As per your requirement I have added one more column of customer ref in the below query.
SELECT DISTINCT
DATEDIFF(DD, T2.DocDueDate, T3.DocDueDate) AS 'Lapsed Days',
CASE
WHEN DATEDIFF(DD, T2.DocDueDate, T3.DocDueDate) >= 5 THEN 'Late by 5 Days or More'
WHEN DATEDIFF(DD, T2.DocDueDate, T3.DocDueDate) BETWEEN 1 AND 4 THEN 'Late by 1 to 4 Days'
WHEN DATEDIFF(DD, T2.DocDueDate, T3.DocDueDate) = 0 THEN 'On Time'
WHEN DATEDIFF(DD, T2.DocDueDate, T3.DocDueDate) < 0 THEN 'Early Shipment'
ELSE 'Not Shipped'
END AS 'Ship Stat Results',
T2.DocNum AS 'Sales Ord',
T2.DocDueDate AS 'Exp Del Date',
T3.DocNum AS 'Del Num',
T3.DocDueDate AS 'Del Date',
T2.CardCode as 'BP Code',
T2.Cardname as 'BP Name',
T4.GroupCode as 'BP Group',
T5.GroupName as 'BP Group Name',
T2.NumAtCard as 'Customer Ref No'
FROM RDR1 T0
LEFT OUTER JOIN DLN1 T1
ON T1.BaseType = 17
AND T0.DocEntry = T1.BaseEntry
AND T0.LineNum = T1.BaseLine
AND T0.ItemCode = T1.ItemCode
LEFT OUTER JOIN ORDR T2
ON T0.DocEntry = T2.DocEntry
AND T2.Canceled = 'N'
LEFT OUTER JOIN OCRD T4
on T4.Cardcode = T2.CardCode
INNER JOIN OCRG T5 on T5.GroupCode = T4.GroupCode
LEFT OUTER JOIN ODLN T3
ON T1.DocEntry = T3.DocEntry
WHERE
T2.DocType = 'I'
ORDER BY
DATEDIFF(DD, T2.DocDueDate, T3.DocDueDate),
T2.DocDueDate DESC,
T2.DocNum,
T3.DocDueDate,
T3.DocNum
FOR BROWSE
Thanks,
Kuldeep