Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Pull Multiple Phone Records On Contact Row
Message
From
08/05/2010 14:30:57
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01463756
Message ID:
01463831
Views:
35
>>>Hi Kev,
>>>
>>>Something's not right. Here's my queries
>>>
>>>
>>>SELECT RecordId, FirstName, LastName FROM Contacts WHERE LastName = 'marois'
>>>RecordId    FirstName   LastName
>>>-------------------------------------------------------
>>>4010        Kevin      Marois
>>>
>>>
>>>
>>>SELECT * FROM Phones WHERE ContactId = 4010
>>>RecordId    ContactId   PhoneTypeId PhoneNum
>>>----------- ----------- ----------- --------------------------------------------------
>>>58920       4010        4           +19513920229
>>>58921       4010        1           +19512233208
>>>
>>>SELECT * FROM PickLists WHERE ItemType = 'phone'
>>>RecordId    Caption                        ItemType
>>>----------- ------------------------------ --------------------
>>>1           Home                           phone
>>>2           Office                         phone
>>>3           FAX                            phone
>>>4           Cell                           phone
>>>5           Other1                         phone
>>>6           Other2                         phone
>>>7           Other3                         phone
>>>
>>>SELECT ContactId, [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
>>>	WHERE ContactId = 4010
>>>
>>>ContactId   Home            Office     Fax		  Cell            Other1     Other2     Other3
>>>----------- --------------- ---------- ---------- --------------- ---------- ---------- ----------
>>>4010        NULL            NULL       NULL       +19513620229    NULL       NULL       NULL
>>>4010        +19512433208    NULL       NULL       NULL            NULL       NULL       NULL
>>>
>>>
>>>Notice that I'm getting back 2 rows. Never used Pivot tables before, so I'm not entirely sure what's wrong.
>>
>>I must be missing something. I only see two records in the phone table (1 and 4) and that matches your final result. What should you be getting in your final resultset?
>
>I think Kevin was expecting (rightly) just one row since it's the same ContactId ?
>But I'm useless at SQL so I've no idea why that's not happening :-}

Oh, that flew right by me....
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Reply
Map
View

Click here to load this message in the networking platform