Pivot SQL z łańcuchem

Mam dwie tabele w SQL Server: Klient i adres

Tabela klientów:

<code>CustomerID  FirstName  LastName
----------- ---------- ----------
1           Andrew     Jackson         
2           George     Washington
</code>

Tabela adresów:

<code>AddressID   CustomerID  AddressType City
----------- ----------- ----------- ----------
1           1           Home        Waxhaw     
2           1           Office      Nashville    
3           2           Home        Philadelphia
</code>

To jest wyjście, którego potrzebuję:

<code>CustomerID  Firstname  HomeCity      OfficeCity
----------- ---------- ----------    ----------
1           Andrew     Waxhaw        Nashville
2           George     Philadelphia  Null
</code>

To jest moje zapytanie, ale nie uzyskuję właściwego wyniku:

<code>SELECT CustomerID, Firstname, HOme as HomeCity, Office as OfficeCity FROM 
   (SELECT C.CustomerID, C.FirstName, A.AddressID, A.AddressType, A.City 
    FROM Customer C, Address A 
    WHERE C.CustomerID = A.CustomerID)as P
PIVOT (MAX(city) FOR AddressType in ([Home],[Office])) as  PVT
</code>

To jest wynik, który otrzymuję:

<code>CustomerID  Firstname  HomeCity      OfficeCity
----------- ---------- ----------    ----------
1           Andrew     Waxhaw        NULL
1           Andrew     NULL          Nashville
2           George     Philadelphia  Null
</code>

Jak widać, klient 1 pojawia się dwukrotnie w wyniku końcowym. Czy możliwe jest uzyskanie tylko jednego wiersza na klienta?

Sprawdziłem ten przykład, ale nie pomogłem: http: //stackoverflow.com/questions/6267660/sql-query-to-convert-rows-into-columns

Dzięki

questionAnswers(2)

yourAnswerToTheQuestion