Calculate the Days Between First and Last Orders

  • Comments posted to this topic are about the item Calculate the Days Between First and Last Orders

  • Nice question to start the week on, thanks Kathi

    ____________________________________________
    Space, the final frontier? not any more...
    All limits henceforth are self-imposed.
    “libera tute vulgaris ex”

  • Ah well.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Good one!

    Until I understood the framing context, I used to do the LAST_VALUE as FIRST_VALUE(.. ORDER BY [col] DESC), which works out the same, but is likely a worse approach from a query plan perspective. At least if you have indexes in place to support the ordering.


    Just because you're right doesn't mean everybody else is wrong.

  • Or you could do it totally old school by MIN(OrderDate), MAX(OrderDate) and GROUP BY CusomerID. And it would be faster, too.

    Is there any reason to overcomplicate things?

  • In this case, it would work. I should probably have pulled a different column like the amount. I wanted to show that you can use FIRST_VALUE and LAST_VALUE to pull values from the first and last rows. It's not always the same as MIN and MAX.

  • Yes, amount of first and last customer purchase would require a different solution. Sorry for my rant then.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply