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

Need max date from duplicare records. Expand / Collapse
Author
Message
Posted Tuesday, July 9, 2013 9:07 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, May 3, 2014 10:13 PM
Points: 20, Visits: 56
Hello,
I have one table which have 3 columns

dt1 dt2 dt3 dt4 dt5
3/31/2011 3/30/2011 4/4/2011 3/30/2011 4/4/2011
10/24/2012 10/23/2012 10/27/2012 10/23/2012 10/27/2012
12/10/2012 12/8/2012 12/19/2012 12/8/2012 12/19/2012
12/20/2012 12/8/2012 12/19/2012 12/8/2012 12/19/2012
1/16/2013 1/14/2013 1/24/2013 1/14/2013 1/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??
Post #1471685
Posted Tuesday, July 9, 2013 9:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:57 PM
Points: 13,058, Visits: 11,884
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1471696
Posted Tuesday, July 9, 2013 9:43 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, July 17, 2014 3:21 PM
Points: 185, Visits: 917
MAX

GROUP BY



Alex Suprun
Post #1471715
Posted Tuesday, July 9, 2013 7:34 PM


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 @ 8:28 PM
Points: 3,609, Visits: 5,219
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!

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 #1471912
Posted Friday, July 12, 2013 10:14 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 10:17 AM
Points: 31, Visits: 278
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.
Post #1473138
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse