AutoHotkey Community

It is currently May 27th, 2012, 12:06 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: February 1st, 2011, 3:34 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
I have one PERSONS table and one PHONE table. Each person has several rows in the phone table (Direct line, fax, cell phone). I can't figure out how to list all 3 types of phones for each person i.e. how can I convert multiple rows into multiple columns. Hopefully this can clarify what I mean:

Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2011, 5:06 pm 
Offline

Joined: October 4th, 2006, 2:15 am
Posts: 250
Location: Louisville, KY
There are a lot of ways to do this. I'll show you two.

Code:
select p1.name,
         ( select phone_number from phone p2 where p2.person_id = p1.person_id and phone_type = 'direct' ) 'direct',
         ( select phone_number from phone p2 where p2.person_id = p1.person_id and phone_type = 'fax' )    'fax',
         ( select phone_number from phone p2 where p2.person_id = p1.person_id and phone_type = 'cell' )   'cell'
from persons p1


faster but tricky...
Code:
select p1.name,
         max( case when phone_type = 'direct' then phone_number else '' end) 'direct',
         max( case when phone_type = 'fax' then phone_number else '' end)    'fax',
         max( case when phone_type = 'cell' then phone_number else '' end)   'cell'
from persons p1,
        phone p2
where p2.person_id = p1.person_id
group by p1.name


There are many other ways... look at CROSSTAB.

Untested so there might be a typo or two. (In fact, I've edited to correct a couple already :) )


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2011, 5:16 pm 
Offline

Joined: October 4th, 2006, 2:15 am
Posts: 250
Location: Louisville, KY
Actually, that second query won't work if the person has no phone numbers at all. It needs an outer join. Instead of constantly editing, I'll correct it here:
Code:
select p1.name,
         max( case when phone_type = 'direct' then phone_number else '' end) 'direct',
         max( case when phone_type = 'fax'    then phone_number else '' end) 'fax',
         max( case when phone_type = 'cell'   then phone_number else '' end) 'cell'
from persons p1
left outer join phone p2
on p2.person_id = p1.person_id
group by p1.name


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2011, 8:28 am 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
wtg: Thank you so much for this, I eventually got help from someone nicked Xgc on IRC yesterday and this was the result:

Code:
SELECT persons.firstname, persons.lastname, p1.phone AS Direct_Number, p2.phone as Fax_Number, p3.phone as Cell_Number
FROM persons LEFT JOIN
phone AS p1 ON person.person_id = p1.person_id AND p1.type = 'direct' left join
phone AS p2 ON person.person_id = p2.person_id AND p2.type = 'fax' left join
phone AS p3 ON person.person_id = p3.person_id AND p3.type = 'cell'


I really appreciate your reply and I'm definetly going to try to understand your suggested solutions to see if there are any advantages. I learn some bits of SQL here and there as I go, but I'm beginning to grasp how much more there is to learn.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group