I have two tables:
Customers -> CustomerId, Name
Phones -> PhoneId , CustomerId , Phone
This tables are join through CustomerId field. Every customer have more than one phone in phones table. I need to get only one of them. How can i do this.
SELECT
Customers.CustomerId ,
Customers.Name ,
Phones.Phone
FROM Customers
INNER JOIN Phones ON Customers.CustomerId = Phones.CustomerId
In this case i get something like:
+----------+----+-----------+
|CustomerId|Name|Phone |
+----------+----+-----------+
|1010 |Jack|999-999-999|
+----------+----+-----------+
|1010 |Jack|888-888-888|
+----------+----+-----------+
|1010 |Jack|111-111-111|
+----------+----+-----------+
|2020 |Pit |123-123-123|
+----------+----+-----------+
|2020 |Pit |321-321-321|
+----------+----+-----------+
But i need only one customer and one phone number like:
+----------+----+-----------+
|CustomerId|Name|Phone |
+----------+----+-----------+
|1010 |Jack|888-888-888|
+----------+----+-----------+
|2020 |Pit |123-123-123|
+----------+----+-----------+
I tried to do it using subquery:
SELECT
c.CustomerId ,
c.Name ,
SELECT ( p.Phone FROM Phones WHERE p.CustomeId=c.CustomerId FETCH FIRST 1 ROWS ONLY)
FROM Customers c
But it works VERYYYY SLLOOOW thats why i cant use subquery. How can i do it anyway else?