Latest value based on last modified date

  • Hi All,

    I have a table that holds the following data:

    ProjectsCustomersUpdatedByNewValueUpdatedOn

    Project 1Customer AUser 1£1,142.0005/01/2012 10:27

    Project 1Customer AUser 1£1,839.0005/01/2012 10:59

    Porject 2Customer BUser 5£121,995.0024/05/2012 09:26

    Project 3Customer CUser 3£20,332.0026/06/2012 09:57

    Project 4Customer DUser 2£18,191.0014/05/2012 13:40

    Project 4Customer DUser 1£46,906.0018/05/2012 13:09

    Project 4Customer DUser 3£58,309.0018/06/2012 13:39

    Project 4Customer DUser 1£76,500.0019/06/2012 15:44

    Project 4Customer DUser 1£88,112.0003/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:

    ProjectsCustomersUpdatedByNewValueUpdatedOn

    Project 1Customer AUser 1£1,839.0005/01/2012 10:59

    Porject 2Customer BUser 5£121,995.0024/05/2012 09:26

    Project 3Customer CUser 3£20,332.0026/06/2012 09:57

    Project 4Customer DUser 1£88,112.0003/07/2012 14:14

    Your help is highly appreciated and thanks in advance 🙂

  • Adelphi (7/23/2012)


    Hi All,

    I have a table that holds the following data:

    ProjectsCustomersUpdatedByNewValueUpdatedOn

    Project 1Customer AUser 1£1,142.0005/01/2012 10:27

    Project 1Customer AUser 1£1,839.0005/01/2012 10:59

    Porject 2Customer BUser 5£121,995.0024/05/2012 09:26

    Project 3Customer CUser 3£20,332.0026/06/2012 09:57

    Project 4Customer DUser 2£18,191.0014/05/2012 13:40

    Project 4Customer DUser 1£46,906.0018/05/2012 13:09

    Project 4Customer DUser 3£58,309.0018/06/2012 13:39

    Project 4Customer DUser 1£76,500.0019/06/2012 15:44

    Project 4Customer DUser 1£88,112.0003/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:

    ProjectsCustomersUpdatedByNewValueUpdatedOn

    Project 1Customer AUser 1£1,839.0005/01/2012 10:59

    Porject 2Customer BUser 5£121,995.0024/05/2012 09:26

    Project 3Customer CUser 3£20,332.0026/06/2012 09:57

    Project 4Customer DUser 1£88,112.0003/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.

  • I would be grateful if you could help me with query

    Thanks

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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.

Viewing 5 posts - 1 through 4 (of 4 total)

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