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 |
|
VJG
Starting Member
17 Posts |
Posted - 2012-03-08 : 10:54:59
|
| Guys, i'm new the site, seen great info so far.I'm having a hard time combining Student Contacts numbers into a single row.My tables look like this:Table1.PhoneContact ID | Phone Type | Phone | Ext123 W 7418529632 4512123 W2 7412583698 4125123 C1 7411235487 null123 H 7414562356 null152 W 1254789542 null152 C2 1235872135 null162 C1 1253684451 nullTable2.ContactStudent_id | Contact_id | FName | LName | Relation00123000 123 Joe Jones Father00123000 152 Mary Jones Mother00162000 162 Danny Smith FatherTable3. StudentStudent_ID | FName | LName | Current_status00123000 Mike Jones A00162000 Eddie Smith AWhat I'm needing is to get the Student_ID, Contact_ID, Cell number, if they have one(C1 or C2),Work number, if they have one(W or W1) and Ext if Contact has a Work Phone for all active students. Note: W and C1 take precedence or W1 and C2. desired results:Student_id | Contact_id | CellPhone | WorkPhone | Ext00123000 123 7411235487 7418529632 451200123000 152 1235872135 1254789542 null00162000 162 1253684451 null nullWhat I've tried using case statements, but I end up getting multiple rows, one for the cellphone and another for the workphone.Many Thanks to all who give feedback. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 10:58:28
|
| so you want only one workphone and one cell phone to be listed for each contact?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
VJG
Starting Member
17 Posts |
Posted - 2012-03-08 : 11:12:19
|
| Yes, that is correct. Only need one. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 11:37:34
|
| [code]SELECT c.Student_id ,c.Contact_ID,MAX(CASE WHEN LEFT([Phone Type],1)='W' THEN Phone END) AS WorkPhone,MAX(CASE WHEN LEFT([Phone Type],1)='C' THEN Phone END) AS CellPhone,MAX(CASE WHEN LEFT([Phone Type],1)='W' THEN Ext END) AS extFROM Contact cINNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY ContactID,LEFT([Phone Type],1) ORDER BY [Phone Type]) AS Rn,* FROM Phone )pON p.ContactID = c.Contact_IDAND p.Rn=1GROUP BY c.Student_id ,c.Contact_ID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
VJG
Starting Member
17 Posts |
Posted - 2012-03-08 : 11:43:31
|
| Thanks! I'll give this a try. Ill post back. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-08 : 11:59:41
|
| welcome...------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
VJG
Starting Member
17 Posts |
Posted - 2012-03-08 : 12:42:16
|
| Visakh16 Thank you!!! it was exactly what i need! |
 |
|
|
|
|
|
|
|