I have a large(ish) DB. Simply put customer records. Now I have two tables; one is CustomerInfo
and PhoneNumbers
. Some sample data would be,
CustomerInfo
`````````````
CustID | CustName | CustomerLocation
--------+---------------+--------------------
1 | Paul | Bristol
2 | Eugin | Bournemouth
3 | Francis | London
PhoneNumbers
````````````
PhoneID | CustID | PhoneNumber
--------+-----------+----------------
1 | 1 | 0117123456
2 | 2 | 0120212345
3 | 2 | 0784256864
4 | 3 | 0204587895
Now as you could see, Paul
and Francis
have just one number, but Eugin
has two. In a normal world, if I join the two tables as,
SELECT
c.CustName,
p.PhoneNumber
FROM
CustomerInfo c
JOIN
PhoneNumbers p
ON c.CustID = p.CustID
I would get,
CustName | PhoneNumber
------------+--------------------
Paul | 0117123456
Eugin | 0120212345
Eugin | 0784256864
Francis | 0204587895
This is right, but I am running another Query that need the result to be,
CustName | PhoneNumber1 | PhoneNumber2
------------+-------------------+---------------
Paul | 0117123456 | NULL
Eugin | 0120212345 | 0784256864
Francis | 0204587895 | NULL
I could write a table variable from a function. But as this is going to be part of a Query, I was hoping if there were any other solution.
EDIT - I would like to highlight the section, as this is going to be part of a Query, I was hoping if there were any other solution
, The actual query is going to be,
SELECT
per.[PersonId],
per.[ClientReference],
sal.SalutationName,
per.[FirstName],
per.[LastName],
per.[DateOfBirth],
per.[Password]
FROM
[Customers].[people].[Person] per
JOIN
[Customers].[people].[Salutation] sal
ON sal.SalutationId = per.SalutationId
What I would like is,
SELECT
per.[PersonId],
per.[ClientReference],
sal.SalutationName,
per.[FirstName],
per.[LastName],
per.[DateOfBirth],
per.[Password],
pn.[PhoneNumber1], --Made up column, there is only one column in the pn table
pn.[PhoneNumber2] --Made up column, there is only one column in the pn table
FROM
[Customers].[people].[Person] per
JOIN
[Customers].[people].[Salutation] sal
ON sal.SalutationId = per.SalutationId
JOIN
[Customers].[comms].[PhoneNumber] pn
ON per.PersonId = pn.PersonId