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.
| Author |
Topic |
|
mica
Starting Member
5 Posts |
Posted - 2007-10-10 : 14:04:30
|
| Should be a pretty basic question:I have two tables: people and telecommunication. telecommunication contains all the phone numbers for records in the people table. I need a query that will return all of the records in the people table but will also return a phone number from the telecommunications table only if it has a code of 'mobile'. So there may be p1, p2, p3, p4, p5 and only p4 has a mobile number, I need the result set to show:person phonep1p2p3p4 m1p5Thanks, |
|
|
Kristen
Test
22859 Posts |
|
|
mica
Starting Member
5 Posts |
Posted - 2007-10-10 : 14:40:22
|
| Like this?Table People (p)people_id....last_name101..........Smith102..........Jones103..........Williams104..........Jefferson105..........MillsTable Telecommunications (t)Tele_id....people_id....tele_type....tele_number1...............101.........home........123-456-78942...............102.........office......123-548-85473...............103.........office1.....144-545-85714...............104.........mobile......124-251-41245...............105.........fax.........124-854-8574Expected results:p.people_id....p.last_name....t.tele_typet.....tele_number101..............Smith 102..............Jones103..............Williams104..............Jefferson..........mobile............124-251-4124105..............Mills |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-10 : 15:21:16
|
| Well, sort of. But from that starting point I will have to create a table, write the Insert statements, and then I can start writing the query. Right now I don't have time to do all that, sorry.Kristen |
 |
|
|
mica
Starting Member
5 Posts |
Posted - 2007-10-10 : 15:49:59
|
| I see. Sorry for the confusion:create table people(people_id int,last_name varchar(10))insert into people (people_id, last_name)select 101, 'smith'union allselect 102, 'jones'union allselect 103, 'williams'union allselect 104, 'jefferson'union allselect 105, 'mills'create table telecom(tele_id int,people_id int,tele_type varchar(10),tele_number int)insert into telecom (tele_id, people_id, tele_type, tele_number)select 1,101,'home',1234567894 union allselect 2,102,'office',1235488547 union allselect 3,103,'office',1445458571 union allselect 4,104,'mobile',1242514124 union allselect 5,105,'fax',1248548574 |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-10 : 16:01:02
|
| This should do itDECLARE @people table(people_id int, last_name varchar(10))insert into @people (people_id, last_name)select 101, 'smith'union allselect 102, 'jones'union allselect 103, 'williams'union allselect 104, 'jefferson'union allselect 105, 'mills'declare @telecom table (tele_id int,people_id int,tele_type varchar(10),tele_number int)insert into @telecom (tele_id, people_id, tele_type, tele_number)select 1,101,'home',1234567894 union allselect 2,102,'office',1235488547 union allselect 3,103,'office',1445458571 union allselect 4,104,'mobile',1242514124 union allselect 5,105,'fax',1248548574select p.people_id ,p.last_name ,'tele_type' = CASE WHEN t.tele_type = 'mobile' THEN 'mobile' ELSE null END ,'tele_number' = CASE WHEN t.tele_type = 'mobile' THEN t.tele_number ELSE null ENDfrom @people pleft join @telecom ton p.people_id = t.people_idJim |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-10 : 16:22:12
|
You can move the CASE inot the OUTER JOIN. May make some small difference to Perfomance, I suppose ...SELECT P.people_id, P.last_name, T.tele_type, T.tele_numberFROM @people AS P LEFT JOIN @telecom AS T ON T.people_id = P.people_id AND T.tele_type = 'mobile' Kristen |
 |
|
|
mica
Starting Member
5 Posts |
Posted - 2007-10-10 : 16:36:15
|
| Thanks, Jim. What about this scenario where people_id 104 has two telephone numbers and I only want to return the one with mobile.DECLARE @people table(people_id int, last_name varchar(10))insert into @people (people_id, last_name)select 101, 'smith'union allselect 102, 'jones'union allselect 103, 'williams'union allselect 104, 'jefferson'union allselect 105, 'mills'declare @telecom table (tele_id int,people_id int,tele_type varchar(10),tele_number int)insert into @telecom (tele_id, people_id, tele_type, tele_number)select 1,101,'home',1234567894 union allselect 2,102,'office',1235488547 union allselect 3,103,'office',1445458571 union allselect 4,104,'mobile',1242514124 union allselect 6,104,'office',1425474 union allselect 5,105,'fax',1248548574select p.people_id,p.last_name,'tele_type' = CASE WHEN t.tele_type = 'mobile' THEN 'mobile' ELSE null END,'tele_number' = CASE WHEN t.tele_type = 'mobile' THEN t.tele_number ELSE null ENDfrom@people pleft join@telecom tonp.people_id = t.people_idExpected results:p.people_id....p.last_name....t.tele_typet.....tele_number101..............Smith 102..............Jones103..............Williams104..............Jefferson..........mobile............124-251-4124105..............Mills |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-10 : 16:42:52
|
| "What about this scenario where people_id 104 has two telephone numbers and I only want to return the one with mobile"I think my query works OK for that scenario.Kristen |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-10 : 17:08:03
|
| Probably should have done it this way anywayselect p.people_id,p.last_name,t.tele_type,t.tele_number from@people pleft join (select * from @telecom where tele_type = 'mobile' )tonp.people_id = t.people_id Jim |
 |
|
|
mica
Starting Member
5 Posts |
Posted - 2007-10-10 : 17:21:35
|
| Works great, Kristen.Thanks for your patience, guys. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-11 : 00:40:37
|
@jimf:What does the sub-select get you?Won't just joining to @telecom do the trick? (and be more readable )Kristen |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-11 : 06:07:32
|
| It gets me thinking that my I.Q. is inversely proportional to amount of time spent at work!Jim |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-11 : 06:48:37
|
Ah, OK. Want to borrow my T-Shirt? |
 |
|
|
|
|
|
|
|