Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 join 2 tables select count = 1

Author  Topic 

cwfontan
Yak Posting Veteran

87 Posts

Posted - 2009-09-11 : 09:37:15
I have 2 tables 1 has cities and states the other has city state and zip..

I want to return the zips that match city state of table 1 where table 2 only has 1 zip for that city


--?? select t1.city, t1.state, t2.zipcode
--?? FROM table1 t1 Join t2 on t1.city=t2.city and t1.state=t2.state

but just for the cities that only have 1 zip..

multiple cities with 1 zip is ok
but not 1 city with multiple zips



table1
ID | City |State
469 HARVEY IL
470 HARTVILLE OH
471 HARRISON OH
472 HARLEYSVILLE PA
473 HAMMOND IN
474 HAMILTON ON
475 HAMILTON ON
476 HAMILTON OH
477 HALIFAX NS
478 GUELPH ON
479 GROVEPORT OH
480 GROVE CITY OH
481 GREENVILLE OH
482 GREENVILLE OH
483 GREENVILLE PA

table2

ZipCode|City |State
12138 North Petersburg NY
12138 Petersburg NY
12138 Petersburgh NY
12138 Taconic Lake NY
12139 Arietta NY
12139 Piseco NY
12140 Poestenkill NY
12141 Quaker Street NY
12143 Ravena NY
12144 Defreestville NY
12144 Rensselaer NY
12147 Rensselaerville NY
12147 Rensselaervle NY
12148 Rexford NY
12148 Vischer Ferry NY
12149 Richmondville NY
12149 West Richmondville NY
12150 Rotterdam Jct NY
12150 Rotterdam Junction NY

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-11 : 09:43:17
Can you please give sample data that is fitting your requirements?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

cwfontan
Yak Posting Veteran

87 Posts

Posted - 2009-09-11 : 10:10:52
quote:
Originally posted by webfred

Can you please give sample data that is fitting your requirements?







Sorry maybe im not clear.

update table1 where t1.city = t2.city AND t1.state=t2.state AND there is only 1 count of that city in t2

Table1(customerData)
ID | city | state | zip
3 greenville OH NULL
4 Dublin OH NULL


Table2 (zipCodeLookup)

greenville OH 04452
Alexandria OH 43001
Amlin OH 43002
Ashley OH 43003
Blacklick OH 43004
Bladensburg OH 43005
Brinkhaven OH 43006
Broadway OH 43007
Buckeye Lake OH 43008
Cable OH 43009
Catawba OH 43010
Centerburg OH 43011
Croton OH 43013
Danville OH 43014
Delaware OH 43015
Dublin OH 43016
Dublin OH 43017
Etna OH 43018
Fredericktown OH 43019
Fredricktwn OH 43019
Galena OH 43021


Update table1
with zipcode from table2 where t1.city=t2.city & t1.state=t2.state & count(t2.city) = 1

Result
-------
id city state zipcode
3 Greensville OH 04452
4 Dublin OH NULL --Because it had 2 zips for same city

Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-09-11 : 10:33:25
Sample Data

declare @r table (id int,city varchar(40), [state] varchar(2), zip varchar(5))
insert @r
select 3,'greenville','OH',null union all
select 4,'Dublin','OH',null union all
select 5,'Etna','OH',null

declare @t table (city varchar(40), [state] varchar(2), zip varchar(5))
insert @t
select 'greenville', 'OH', '04452' union all
select 'Alexandria', 'OH', '43001' union all
select 'Dublin', 'OH', '43016' union all
select 'Dublin', 'OH', '43017' union all
select 'Etna', 'OH', '43018'


Query

update a
set a.zip = b.zip
from @r a inner join
(select city,max(zip) as zip,max([state]) as [state]
from @t group by city
having count(city) = 1
) b
on a.city = b.city and a.state = b.state


Result

id          city                                     state zip
----------- ---------------------------------------- ----- -----
3 greenville OH 04452
4 Dublin OH NULL
5 Etna OH 43018
Go to Top of Page
   

- Advertisement -