Well I have tried writing the query like this:
SELECT ORDER_NO, MAX(PROMISE_DT) , SUM(REVENUE), SUM(COST)
FROM OPENORDERS
GROUP BY ORDER_NO
And it still returns 2 rows for the ones that have multiple promise dates... the max isn't working.
Are you saying just write my query normally and just do the inner join you wrote?
Main Topics
Browse All Topics





by: ksaulPosted on 2007-05-22 at 07:13:19ID: 19134049
You also have different Revenue and Cost data for each row and if you want to retrieve those along with the max promise date you would have to get the aggregate in a subquery and then select the orders that match those dates. Like:
SELECT *
FROM YourOrderTable
INNER JOIN (SELECT [ORDER NUMBER], MAX([PROMISE DATE]) AS MaxDate
FROM YourOrderTable
GROUP BY [ORDER NUMBER]) AS MaxDates
ON YourOrderTable.[ORDER NUMBER] = MaxDates.[ORDER NUMBER] AND YorOrderTable.[PROMISE DATE] = MaxDates.MaxDate