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
 General SQL Server Forums
 New to SQL Server Programming
 Phone number list by location

Author  Topic 

jchoudja
Starting Member

41 Posts

Posted - 2013-02-22 : 11:38:29
Hi, I am trying to get a phone number list by location for each member.

Member
MemberID|MemberName
--------------------
1_______|_Bean
2_______|_Jonathan
3_______|_Peter
4_______|_Ashley

memberPhone
MemberPhoneID|MemberID|PhoneID
-------------------------------
M1___________|_1______|p1
M2___________|_1______|P2
M3___________|_2______|P3
M4___________|_3______|P4
M5___________|_3______|P5
M6___________|_3______|P6


Phone
PhoneID|PhoneLocationID|PhoneNumber|
-------------------------------
p1_____|_L1____________|_281 520 4444
P2_____|_L3____________|_281 485 6656
P3_____|_L1____________|_713 852 5253
P4_____|_L1____________|_832 525 3333
P5_____|_L3____________|_281 785 5225
P6_____|_L5____________|_281 856 2323


PhoneLocation
PhoneLocationID|PhoneLocationName|PhonelLocationprecedence
L1_____________|CellPhone 1______|_6
L2_____________|CellPhone 2______|_5
L3_____________|Workphone 1______|_3
L4_____________|Workphone 2______|_4
L5_____________|Homephone 1______|_1
L6_____________|Homephone 2______|_2
L7_____________|Otherphone_______|_7

I want to get a table like this: No Duplicate Rows. Each memeber should be in 1 row and all his phone numbers listed in the appropriate colum when thay are available.

MemberPhonelist
MemberID|MemberName|CellPhone_____|HomePhone_____|WorkPhone_____|Otherphone
---------------------------------------------------------------------------
1_______|_Bean_____|_281 520 4444_|______________|_281 485 6656_|__________
2_______|_Jonathan_|_713 852 5253_|______________|______________|__________
3_______|_Peter____|_832 525 3333_|_281 785 5225_|_281 856 2323_|__________
4_______|_Ashley___|______________|______________|______________|__________

I have done someting like this:
CASE WHEN PhoneLocationPrecedence IN (1 , 2) THEN PhonePhoneNumber ELSE '-' END AS homephone
CASE WHEN PhoneLocationPrecedence IN (5 , 6) THEN PhonePhoneNumber ELSE '-' END As Cellphone
CASE WHEN PhoneLocationPrecedence IN (3 , 4) THEN PhonePhoneNumber ELSE '-' END AS workphone
But I am getting only one phone numer per row. If a member has more than 1 it will show phone numbers in many rows

Help please.


jc

jchoudja
Starting Member

41 Posts

Posted - 2013-02-22 : 11:39:43
Please read __ in my tables as Blancks


jc
Go to Top of Page

jchoudja
Starting Member

41 Posts

Posted - 2013-02-22 : 11:41:36
For now, you can ignore the other Phone location.

jc
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-02-22 : 17:52:05
How do you intend multiple phones of the same type (e.g., CellPhone1 and CellPhone2) to be handled in the returned data? Should they be listed in separate columns? Concatenated together in one column? Other? In any event, you might want to make use of the PIVOT operator to transform the vertical list of phone number into a horizontal (single row) list. BOL has details.

=================================================
There are two kinds of light -- the glow that illuminates, and the glare that obscures. -James Thurber
Go to Top of Page

jchoudja
Starting Member

41 Posts

Posted - 2013-02-25 : 07:43:34
Phone numebers should be listed in seperate columns like in MemberPhonelist
table above.


jc
Go to Top of Page

jchoudja
Starting Member

41 Posts

Posted - 2013-02-25 : 09:15:26
I made is simple here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=183258
and I don't know how to remove this post.


jc
Go to Top of Page
   

- Advertisement -