Need max date from duplicare records.

  • Hello,

    I have one table which have 3 columns

    dt1 dt2 dt3 dt4 dt5

    3/31/20113/30/20114/4/20113/30/20114/4/2011

    10/24/201210/23/201210/27/201210/23/201210/27/2012

    12/10/201212/8/201212/19/201212/8/201212/19/2012

    12/20/201212/8/201212/19/201212/8/201212/19/2012

    1/16/20131/14/20131/24/20131/14/20131/24/2013

    when there comes duplicate record in dt2,dt3,dt4,dt5 then i just need max dt from dt1

    else null

    so can you please help me??

  • First things first. We need some consumable ddl and sample data. I created this for you this time so you can see what I mean.

    if OBJECT_ID('tempdb..#Something') is not null

    drop table #Something

    set dateformat mdy

    create table #Something

    (

    dt1 datetime,

    dt2 datetime,

    dt3 datetime,

    dt4 datetime,

    dt5 datetime

    )

    insert #Something

    select '3/31/2011', '3/30/2011', '4/4/2011', '3/30/2011', '4/4/2011' union all

    select '10/24/2012', '10/23/2012', '10/27/2012', '10/23/2012', '10/27/2012' union all

    select '12/10/2012', '12/8/2012', '12/19/2012', '12/8/2012', '12/19/2012' union all

    select '12/20/2012', '12/8/2012', '12/19/2012', '12/8/2012', '12/19/2012' union all

    select '1/16/2013', '1/14/2013', '1/24/2013', '1/14/2013', '1/24/2013'

    select * from #Something

    So based on your sample data what do you want for desired output? I would like to point out that you do not have any duplicate rows here at all. Can you explain what you mean by that?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • MAX

    GROUP BY


    Alex Suprun

  • As Alexander hinted at:

    SELECT dt1=MAX(dt1), dt2, dt3, dt4, dt5

    FROM #Something

    GROUP BY dt2, dt3, dt4, dt5


    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

  • You have more than 3 columns.

    Have you written any code for this at all? Can you post it? What have you tried?

    As others have said, MAX and Group By will help you.

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

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