Here is the scenario we are working on here. We have a customer table and a sales table. The tables join via the transaction ID in cust table.
A customer can buy any fruit from the store.
What we need to do is find out for example: how many customer bought apples before they bought cherries.
Table structure:
Cust - Cust ID, Transaction ID, ...
Sales - Transaction ID, Fruit ID, Insert date for record, ...
What would be the most cost efficient way of achieving this considering that a customer can have different transaction ID for same fruit ID as he might have bought the fruit multiple times.
So we need to find, when is the 1st apple and 1st cherry bought by cust then check the dates on them.
I second Gary’s opinion of the data model, but that is irrelevant to the question.
This is one possible solution. It is likely to be pretty efficient if there are lots of potential values for FRUIT_ID and that column is indexed.
If there are fewer values of FRUIT_ID then a variation on Gary’s suggestion would probably be more efficient:
Caveat lector: I currently don’t have access to a database so these statements are untested and may have syntax errors. I’ll check them when I can.