eg.
I have 2 tables Customers and Orders
Customers has columns CustomerID, Name
Orders has columns OrderID, OrderedOn
where OrderedOn is a DateTime
Now I want a query which will give me the
CustomerID OrderID and OrderTally
where OrderTally = 'Initial' for the first order OrderTally = 'InMiddle' for everything in the middle and OrderTally = 'Final' if its the last order and was 30 days ago,
I am trying to create a Case statement for OrderTally
and struggling
How do I check if the OrderID is the first or last or in the middle
-- The First Order
CASE WHEN OrderID IN (...)
THEN 'Initial'
-- The Last Order
WHEN OrderID IN (...)
THEN 'Final'
ELSE
'InTheMiddle'
END
I was thinking of writing ranking statements and then check if it rank is one that's the first one and if rank = total count of all orders then last... but this seems a little complicated.
Is there an easy way of doing this?