Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Pull Multiple Phone Records On Contact Row
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01463756
Message ID:
01463763
Views:
57
OK, I created a small table..... and put some rows in...
create table Phones  (Name varchar(50), PhoneTypeID int, PhoneNum varchar(25))
GO

insert into Phones values ('Kevin Goff', 1, '555-1212')
insert into Phones values  ('Kevin Goff', 2, '555-9912')
insert into Phones  values ('Kevin Goff', 3, '555-8803')


insert into Phones values  ('Katy Goff', 1, '777-3923')
insert into Phones values  ('Katy Goff', 2, '777-0133')
insert into Phones values  ('Katy Goff', 3, '777-9845')
insert into Phones  values ('Katy Goff', 4, '777-0203')

insert into Phones  values ('Mike Smith', 1, '888-9090')
insert into Phones  values ('Mike Smith', 5, '812-1023')


insert into Phones  values ('Ron Wilson', 1, '929-0133')
insert into Phones  values ('Ron Wilson', 6, '856-9988')  
Then here is a PIVOT statement. Most people think of PIVOT as something for dollars, but you can also use it to create columns based on text data as well.
select Name, [1] as [Home], [2] as Office, [3] as Fax, [4] as Cell, 
                     [5] as [Other1], [6] as [Other2], [7] as [Other3]
                     from Phones
                     PIVOT ( MIN(PhoneNum) for PhoneTypeID in ([1], [2], [3], [4], [5], [6], [7])) TempList   
Now, there's one thing about PIVOT....you can't (at least to my knowledge) "dynamically" build the columns with the names from your phone type table. (Unless you generate the query using dynamic SQL). PIVOT is static in that sense...but hopefully this should help you. You'll need to adapt it to your situation, but it should be a start.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform