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)
 plz help w/ query

Author  Topic 

Leeoniya
Starting Member

8 Posts

Posted - 2008-03-13 : 18:04:09
I have 3 tables: Entity, EntityAddress, AddressSubType

Entity has fields containing some account info.
EntityAddresses has fields like Address1, City, Zip, etc.
AddressSubType has fields like Home, Business, Office, etc.

each entity can have multiple addresses, so if i do a JOIN on these tables, i will get several records for each entity, 1 per Address. This is not what i would like.

I would like to make a select statement where the resulting table will contain columns like "HomeAddress1", "BusinessCity", "BusinessFax", etc. but only have 1 record per entity in the result.

can someone show me a sample of how this can be done?

thanks,
Leon

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-03-13 : 21:33:22
select * from entity
left outer join
(select address1 as homeaddress from entityaddresses where addresstype='HOME') homeaddress
on homeaddress.entityid=entity.entityid
left outer join
(select city as businesscity, fax as businessfax from entityaddresses where addresstype='BUS') businessaddress
on homeaddress.entityid=entity.entityid

etc.

At least I think that's what you mean.
Go to Top of Page

Leeoniya
Starting Member

8 Posts

Posted - 2008-03-14 : 14:34:53
thats exactly it, thanks.
Go to Top of Page
   

- Advertisement -