I am trying to convert the following query from using a subquery in the SELECT statement to using JOINS instead. I have been working on this a while now without much success.
Select a.CustID, a.CustOrdType, a.OrdDate, (SELECT TOP 1 b.OrdDate FROM OrderTable b WHERE b.CustID = a.CustID AND b.CustOrdType = a.CustOrdType AND b.OrdDate < a.OrdDate ORDER BY OrdDate Desc) AS LastOrdDate
FROM OrderTable a
This code runs extremely slow so I have been trying to use the tips I found on this site to convert the sub select to a JOIN.
I am hoping to get the last order date for a customer and a certain order type just prior to each orders order date.
thanks
Select a.CustID, a.CustOrdType, a.OrdDate, (SELECT TOP 1 b.OrdDate FROM OrderTable b WHERE b.CustID = a.CustID AND b.CustOrdType = a.CustOrdType AND b.OrdDate < a.OrdDate ORDER BY OrdDate Desc) AS LastOrdDate
FROM OrderTable a
This code runs extremely slow so I have been trying to use the tips I found on this site to convert the sub select to a JOIN.
I am hoping to get the last order date for a customer and a certain order type just prior to each orders order date.
thanks