Q:

Days between max date in each group

I want to find out the number of days between each order. I have the date each order was placed and a reference to show if its their first, second third etc order 
What I want to see if how many days there was between order 1 and 2, then how many days between 2 and 3 and so on.
select OrderID, OrderDate, RankOrder
from Orders

Some example data would be:
Order 1 -> 01/01/2015 -> 1
Order 2 -> 03/01/2015 -> 2
Order 3 -> 10/01/2015 -> 3

I'd want the results to have an extra column with the number of days:
Order 1 -> 01/01/2015 -> 1 -> 2
Order 2 -> 03/01/2015 -> 2 -> 7
Order 3 -> 10/01/2015 -> 3 -> 0

The last order would probably look at the current date to work out the datediff.

A:

Because you have the ranking function, this is just a join with date arithmetic.  Using SQL Server's datediff() function, this looks like:
select o.*,
       datediff(day, o.OrderDate,
                coalesce(onext.OrderDate, getdate()) as diff
from orders o left join
     orders onext
     on o.rankorder = onext.rankorder - 1;

The date arithmetic depends on the database.

