February 3, 2020 at 12:00 am
Comments posted to this topic are about the item Calculate the Days Between First and Last Orders
February 3, 2020 at 5:19 am
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”
February 3, 2020 at 6:36 am
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
February 3, 2020 at 8:36 am
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.
February 3, 2020 at 11:54 am
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?
February 3, 2020 at 12:01 pm
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.
February 3, 2020 at 12:12 pm
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