Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 8608

Find closest date in SQL

$
0
0

Hi Experts

 

I have a query which I hope to display the closest ship date on a Purchase Order

 

Unfortunately where I have more than one PO for the same item the MAX clause is showing me the latest and not the closest.

 

Using MIN does not work as it shows dates in the past.  Is there some SQL I can use to show me the closest delivery date to the current date instead of MAX?

 

SELECT T0.[ItemCode], T0.[ItemName], T0.[OnHand]'Physical_Stock', T0.[IsCommited]'On_Sales_Order', T0.[OnOrder]'On_Purchase_Order',

                                CASE WHEN t0.onorder=0 THEN null ELSE max(T2.[ShipDate])

end as 'Expected_Delivery',SUM(T1.[Quantity])'Invoiced_Qty_12_months'

FROM OITM T0 LEFT JOIN INV1 T1 ON T0.ItemCode = T1.ItemCode LEFT JOIN POR1 T2 ON T0.ItemCode = T2.ItemCode

WHERE T0.[ItemCode] like 'COV%'

GROUP BY T0.[ItemCode], T0.[ItemName], T0.[OnHand], T0.[IsCommited], T0.[OnOrder]

 

 

Kind Regards

Geoff L


Viewing all articles
Browse latest Browse all 8608

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>