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 |
|
spakowski
Starting Member
2 Posts |
Posted - 2010-04-27 : 11:09:36
|
| I have a 'phones' table similar to below:phoneID__Filenumber____phonenumber1________1234________12345672________1234________1235678 3________5678________56789014________5678________23456645________5678________56474886________4567________6783562I want to Join with our master table (master.number = phones.number) so that I can display an account name with respective phone numbers within a single row. (like below)name_____filenumber___phone1___phone2____phone3John______1234_______1234567__1235678Bob_______5678_______5678901__2345664____5647488Sam_______4567_______6783562As you can see every filenumber represents one account (ex. John). I was trying to figure out if I could achieve this through the use of PIVOT or a CASE statement or some other way....?Any suggestions or ideas would be greatly appreciated.Thanks,Bob |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 11:16:58
|
| [code]SELECT m.name,n.filenumber,n.phone1,n.phone2,n.phone3FROM master mJOIN(SELECT Filenumber,MAX(CASE WHEN Seq=1 THEN phonenumber ELSE NULL END) AS phone1,MAX(CASE WHEN Seq=2 THEN phonenumber ELSE NULL END) AS phone2,MAX(CASE WHEN Seq=3 THEN phonenumber ELSE NULL END) AS phone3FROM(SELECT ROW_NUMBER() OVER (PARTITION BY Filenumber ORDER BY phoneID) AS Seq,*FROM Table)tGROUP BY Filenumber)nON n.filenumber = m.number [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-27 : 11:18:56
|
| You'll need to know at design time the maximum number of phones a user has. OR, you could use dynamic SQL.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
spakowski
Starting Member
2 Posts |
Posted - 2010-04-27 : 11:30:12
|
| Fantastic! Thanks visakh16, that's exactly what I was looking for!! |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-27 : 12:33:24
|
You also can use concatenating rows for the requirement very easy like this:SELECT M.name, M.filenumber,(SELECT phonenumber + ' ,'FROM phonesWHERE filenumber = M.filenumberORDER BY phonenumberFOR XML PATH('')) AS phone_listFROM Master M |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-27 : 12:35:26
|
quote: Originally posted by ms65g You also can use concatenating rows for the requirement very easy like this:SELECT M.name, M.filenumber,(SELECT phonenumber + ' ,'FROM phonesWHERE filenumber = M.filenumberORDER BY phonenumberFOR XML PATH('')) AS phone_listFROM Master M
I'd do that, as it will work with a varying number of phone numbers per person.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-27 : 12:37:12
|
quote: Originally posted by spakowski Fantastic! Thanks visakh16, that's exactly what I was looking for!!
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|