Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Latest value based on last modified date Expand / Collapse
Author
Message
Posted Monday, July 23, 2012 10:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, December 13, 2013 8:33 AM
Points: 11, 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
Post #1333902
Posted Monday, July 23, 2012 10:12 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 20,857, Visits: 32,877
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.



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)
Post #1333904
Posted Friday, September 13, 2013 4:15 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, December 13, 2013 8:33 AM
Points: 11, Visits: 66
I would be grateful if you could help me with query

Thanks
Post #1494526
Posted Friday, September 13, 2013 4:28 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:53 PM
Points: 3,438, Visits: 5,390
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!
Post #1494529
Posted Friday, September 13, 2013 5:57 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 20,857, Visits: 32,877
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.



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)
Post #1494550
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse