SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Latest value based on last modified date


Latest value based on last modified date

Author
Message
Adelphi
Adelphi
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 66
Hi All,

I have a table that holds the following data:


Projects Customers UpdatedBy NewValue UpdatedOn
Project 1 Customer A User 1 £1,142.00 05/01/2012 10:27
Project 1 Customer A User 1 £1,839.00 05/01/2012 10:59
Porject 2 Customer B User 5 £121,995.00 24/05/2012 09:26
Project 3 Customer C User 3 £20,332.00 26/06/2012 09:57
Project 4 Customer D User 2 £18,191.00 14/05/2012 13:40
Project 4 Customer D User 1 £46,906.00 18/05/2012 13:09
Project 4 Customer D User 3 £58,309.00 18/06/2012 13:39
Project 4 Customer D User 1 £76,500.00 19/06/2012 15:44
Project 4 Customer D User 1 £88,112.00 03/07/2012 14:14


I will need the results to list only one line for project and customer based on the latest modified date (UpdatedOn) regardless of user details but required too. So the results required:


Projects Customers UpdatedBy NewValue UpdatedOn
Project 1 Customer A User 1 £1,839.00 05/01/2012 10:59
Porject 2 Customer B User 5 £121,995.00 24/05/2012 09:26
Project 3 Customer C User 3 £20,332.00 26/06/2012 09:57
Project 4 Customer D User 1 £88,112.00 03/07/2012 14:14

Your help is highly appreciated and thanks in advance :-)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98977 Visits: 38998
Adelphi (7/23/2012)
Hi All,

I have a table that holds the following data:


Projects Customers UpdatedBy NewValue UpdatedOn
Project 1 Customer A User 1 £1,142.00 05/01/2012 10:27
Project 1 Customer A User 1 £1,839.00 05/01/2012 10:59
Porject 2 Customer B User 5 £121,995.00 24/05/2012 09:26
Project 3 Customer C User 3 £20,332.00 26/06/2012 09:57
Project 4 Customer D User 2 £18,191.00 14/05/2012 13:40
Project 4 Customer D User 1 £46,906.00 18/05/2012 13:09
Project 4 Customer D User 3 £58,309.00 18/06/2012 13:39
Project 4 Customer D User 1 £76,500.00 19/06/2012 15:44
Project 4 Customer D User 1 £88,112.00 03/07/2012 14:14


I will need the results to list only one line for project and customer based on the latest modified date (UpdatedOn) regardless of user details but required too. So the results required:


Projects Customers UpdatedBy NewValue UpdatedOn
Project 1 Customer A User 1 £1,839.00 05/01/2012 10:59
Porject 2 Customer B User 5 £121,995.00 24/05/2012 09:26
Project 3 Customer C User 3 £20,332.00 26/06/2012 09:57
Project 4 Customer D User 1 £88,112.00 03/07/2012 14:14

Your help is highly appreciated and thanks in advance :-)


Looks to me like a simple CTE and row_number() function. How about taking this hint and seeing what you can do with it to solve your problem.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Adelphi
Adelphi
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 66
I would be grateful if you could help me with query

Thanks
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18555 Visits: 6431
Since Lynn is too busy serving his country in Afghanistan to come back here to help you out, I'll step in.


WITH SampleData (Projects, Customers, UpdatedBy, NewValue, UpdatedOn) AS (
SELECT 'Project 1','Customer A','User 1',£1142.00,'01/05/2012 10:27'
UNION ALL SELECT 'Project 1','Customer A','User 1',£1839.00,'01/05/2012 10:59'
UNION ALL SELECT 'Project 2','Customer B','User 5',£121995.00,'05/24/2012 09:26'
UNION ALL SELECT 'Project 3','Customer C','User 3',£20332.00,'06/26/2012 09:57'
UNION ALL SELECT 'Project 4','Customer D','User 2',£18191.00,'05/14/2012 13:40'
UNION ALL SELECT 'Project 4','Customer D','User 1',£46906.00,'05/18/2012 13:09'
UNION ALL SELECT 'Project 4','Customer D','User 3',£58309.00,'06/18/2012 13:39'
UNION ALL SELECT 'Project 4','Customer D','User 1',£76500.00,'06/19/2012 15:44'
UNION ALL SELECT 'Project 4','Customer D','User 1',£88112.00,'07/03/2012 14:14')
SELECT *
FROM (
SELECT Projects, Customers, UpdatedBy, NewValue, UpdatedOn
,rn=ROW_NUMBER() OVER (PARTITION BY Projects ORDER BY UpdatedOn DESC)
FROM SampleData) a
WHERE rn=1;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98977 Visits: 38998
Adelphi (9/13/2013)
I would be grateful if you could help me with query

Thanks


I find it interesting that you have waited more than a year to say anything. I am glad to see that Dwain has provided you with a possible solution, but I have to wonder what you have done during this past year and why you didn't come back sooner asking for help.

So I would be interested in knowing what you had tried during this long absence from your question. What problems did you encounter and what do you still not understand. We aren't here to do your work for you, we are here to help you learn and improve your knowledge and skills. You have to remember that what ever code you get from us, or anywhere else on the internet, you have to own. If you don't understand it you should not use it in production. You are the one who has to support it.

And as Dwain indicated, I am currently in Afghanistan, but I still can help when I have the time.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search