First of all, consider replacing max(Order.OrderID) with an expression that
more closely follows the business requirement - e.g.
max(Order.OrderProcessedDateTime) if such a column exists.
Second, are you aware of what this condition actually evaluates to?
WHERE Order.OrderID IN (
SELECT MAX(Order.OrderID)
FROM Order
GROUP BY Order.CustomerID, Order.OrderDate
)
This has little to do with what you've described in your post. Long story
short - there is only a weak correlation between the main query and the
subquery. The subquery returns a list of all highest OrderID values for a
particular CustomerID, and while this may serve your purposes, it would be
better to find a more solid common ground for the two sets.
Since you haven't provided any DDL or sample data, it's difficult to come up
with an exact answer that would help you solve your problem. Please, read
this article:
http://www.aspfaq.com/etiquette.asp?id=5006 Anyway, consider these facts:
1) You need a fully reliable means of determining "the last order per
customer";
2) You need to understand why a specific row should be "flagged", what it
would take to maintain this functionality, and what possible consequences may
arise if this functionality is not properly maintained (i.e. the same could
be achieved through the use of a view, without the need to "flag" rows).
ML
---