Sample Datadeclare @r table (id int,city varchar(40), [state] varchar(2), zip varchar(5))insert @rselect 3,'greenville','OH',null union allselect 4,'Dublin','OH',null union allselect 5,'Etna','OH',null declare @t table (city varchar(40), [state] varchar(2), zip varchar(5))insert @tselect 'greenville', 'OH', '04452' union allselect 'Alexandria', 'OH', '43001' union allselect 'Dublin', 'OH', '43016' union allselect 'Dublin', 'OH', '43017' union allselect 'Etna', 'OH', '43018'
Queryupdate aset a.zip = b.zipfrom @r a inner join (select city,max(zip) as zip,max([state]) as [state]from @t group by cityhaving count(city) = 1) b on a.city = b.city and a.state = b.state
Resultid city state zip----------- ---------------------------------------- ----- -----3 greenville OH 044524 Dublin OH NULL5 Etna OH 43018