SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update using most recent
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

albertkohl
Aged Yak Warrior

USA
738 Posts

Posted - 11/07/2012 :  19:45:08  Show Profile  Visit albertkohl's Homepage  Reply with Quote
Lets say John Smith is in the table 11/7/2012 and 10/7/2012. I want to append the newest date to the file. How would I do that?


update a set
       a.date_added = m.date
from append.dbo.[100_K021034571] a
join main.rez.NEWCONNECTS m
       on SOUNDEX(a.ln) = SOUNDEX(m.ln)
       and SOUNDEX(a.fn) = SOUNDEX(m.fn)
       and left(a.addr,10) = left(m.addr,10)
       and a.zip = m.zip

bandi
Flowing Fount of Yak Knowledge

India
2169 Posts

Posted - 11/07/2012 :  23:40:59  Show Profile  Reply with Quote
update a set
a.date_added = case WHEN m.date > a.date_added THEN m.date ELSE a.date_added end
from append.dbo.[100_K021034571] a
join main.rez.NEWCONNECTS m
on SOUNDEX(a.ln) = SOUNDEX(m.ln)
and SOUNDEX(a.fn) = SOUNDEX(m.fn)
and left(a.addr,10) = left(m.addr,10)
and a.zip = m.zip


--
Chandu
Go to Top of Page

albertkohl
Aged Yak Warrior

USA
738 Posts

Posted - 11/08/2012 :  00:16:01  Show Profile  Visit albertkohl's Homepage  Reply with Quote
i'm sorry, probably didnt explain that well. basically say we have 1 entry in the append.dbo.[100_K021034571] (date is currently NULL) table, but i have 2 entries in the main.rez.NEWCONNECTS table. one from 11/7/2012, and another from 10/7/2012. i basically wanna ensure that append.dbo.[100_K021034571].Date_added = 11/7/2012, and it doesnt use 10/7/2012 instead. make sense?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2169 Posts

Posted - 11/08/2012 :  00:51:12  Show Profile  Reply with Quote
Tr this................

;with cte
as ( SELECT ln, fn, addr, zip, [date], ROW_NUMBER() over(PARTITION BY ln, fn, addr, zip ORDER BY [date] DESC) rn
from main.rez.NEWCONNECTS
)
update a set
a.date_added = c.[date]
from append.dbo.[100_K021034571] a
join cte c
on SOUNDEX(a.ln) = SOUNDEX(c.ln)
and SOUNDEX(a.fn) = SOUNDEX(c.fn)
and left(a.addr,10) = left(c.addr,10)
and a.zip = c.zip
AND c.rn = 1

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000