Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need max date from duplicare records.


Need max date from duplicare records.

Author
Message
sachince61
sachince61
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 76
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??
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16636 Visits: 17024
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)
Alexander Suprun
Alexander Suprun
Old Hand
Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)

Group: General Forum Members
Points: 341 Visits: 1516
MAX

GROUP BY


Alex Suprun
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4279 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Voide
Voide
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 280
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.
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