Extending TSQL ROW_NUMBER to Find MAX Rows or Row Deltas
Some common queries in a database involve grabbing the last value entered into a table for a set of items or comparing a row to its previous row. In the past this involved nasty sub queries one to find the max date and then use that value in outer query. This did not provide the cleanest of code or the best performance. Using the ROW_NUMBER function allows us to simplfy the code and improve performance.
Let's make a CTE first off our query to simplify our code:
The other query involves using row number to look at the current row. This can be handy with running totals or finding deltas between rows.
Let's make a CTE first off our query to simplify our code:
WITH cteTranLog (Item, LogValue, MaxDateNum, Date)To grab the last values entered in a table:
AS
( SELECT Item,
LogValue,
MaxDateNum = ROW_NUMBER() OVER (PARTITION BY Item ORDER BY Date DESC),
Date
FROM dbo.TransactionLog
WHERE ProcessID = 514
)
SELECT Item, LogValue, DateNotice there is only one table seek or scan. A subquery to grab the max date for each item would involve 2 seeks or scans of the table.
FROM cteTranLog
WHERE MaxDateNum = 1 -- we grab the top row only since we are sorted by max date on top
The other query involves using row number to look at the current row. This can be handy with running totals or finding deltas between rows.
SELECT cur.Item, cur.LogValue, cur.Date, prev.Date, prev.LogValueFROM cteTranLog cur
INNER JOIN cteTranLog prev ON (prev.MaxDateNum - 1) = cur.MaxDateNum -- using a -1 because of sort order of desc, dates are descending so 1 is the current making a lesser date 2



Exactly what I was looking for. Thanks a lot!!!
Reply to this
obyjwyfz http://oitapzfj.com pxquoxhe wopnalji
Reply to this
It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something.
Reply to this
Nice post.I like the way you start and then conclude your thoughts. Thanks for this information .I really appreciate your work, keep it up
Reply to this
I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
Reply to this
Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that.
Reply to this
I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day!
Reply to this
I just read through the entire article of yours and it was quite good. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post.
Reply to this
It’s really great post, nice blog..I would like to appreciate your work and would like to tell to my friends.
Thanks for sharing
Business Network Kent | IT support kent
Reply to this
You're dealing with the demon of external validation. You can't beat external validation. You want to know why? Because it feels sooo good.
Reply to this
Each body has its art...
Reply to this
Always seek out the seed of triumph in every adversity.
Reply to this
Life is divided into the horrible and the miserable.
Reply to this
If a dog jumps in your lap, it is because he is fond of you; but if a cat does the same thing, it is because your lap is warmer.
Reply to this
All programmers are playwrights and all computers are lousy actors.
Reply to this
Intimacy is what makes a marriage, not a ceremony, not a piece of paper from the state.
Reply to this
When you have got an elephant by the hind leg, and he is trying to run away, it's best to let him run.
Reply to this
I'm moving, but don't worry! [Someone once] told me we're all on the same planet, so I'll be okay!
Reply to this
I have never been especially impressed by the heroics of people who are convinced they are about to change the world. I am more awed by those who struggle to make one small difference after another.
Reply to this
Rest is the sweet sauce of labor.
Reply to this
The cloning of humans is on most of the lists of things to worry about from Science, along with behaviour control, genetic engineering, transplanted heads, computer poetry and the unrestrained growth of plastic flowers.
Reply to this
Know, first, who you are; and then adorn yourself accordingly.
Reply to this
Feet, why do I need them if I have wings to fly?
Reply to this
Sometime they'll give a war and nobody will come.
Reply to this
One of the keys to happiness is a bad memory.
Reply to this
To lengthen thy life, lessen thy meals.
Reply to this
It's not the hours you put in your work that counts, it's the work you put in the hours.
Reply to this
Even if you're on the right track, you'll get run over if you just sit there.
Reply to this
teophgfp http://ysesflxt.com rekmhwoy cocumath
Reply to this
To do anything truly worth doing, I must not stand back shivering and thinking of the cold and danger, but jump in with gusto and scramble through as well as I can.
Reply to this
Everything of importance has been said before by somebody who did not discover it.
Reply to this
Don't ever take a fence down until you know the reason it was put up.
Reply to this
I love my past. I love my present. I'm not ashamed of what I've had, and I'm not sad because I have it no longer.
Reply to this
We can be sure that the greatest hope for maintaining equilibrium in the face of any situation rests within ourselves.
Reply to this
The dead might as well try to speak to the living as the old to the young.
Reply to this
All programmers are playwrights and all computers are lousy actors.
Reply to this