2

My SQL skills aren't great hence the post.

I'm trying to get all the contact names based on a company out.

For example I have two statements:

Select Id, CompanyName, Address From Clients
Select ClientId, ContactName From Contacts

You may have many contacts to a single client

Result: (I need all the contact names in a single column)

ContactName         Company    Address
----------------------------------------
Johh, Steve         123 Comp   12345 Address
David,Mike, Sarah   44 Comp    111 Address

A working example would be very much appreciated.

1
  • 3
    What RDBMS? Doing this is non-standard-sql, but many RDBMS have functions to do this. Commented Nov 8, 2012 at 18:20

4 Answers 4

1
SELECT DISTINCT (
SELECT ISNULL(ct.ContactName, '') + ', '
FROM dbo.Clients cl JOIN dbo.Contacts ct ON cl.Id = ct.ClientId
WHERE cl.ID = cl2.Id
FOR XML PATH('')) AS ContactName, CAST(cl2.Id AS nvarchar(7)) + ' ' + cl2.CompanyName AS Company, Address
FROM dbo.Clients cl2
ORDER BY 2

Demo on SQLFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

Firstly build all the Contact Names for a Company into a Single Column. Assuming the database to be SQL Server, I'm using a Common Table Expression to store the single column contact list. Once the CTE is built, join it with the Clients table to get the ContactNames. FOR XML is used to concatenate rows.

WITH CTEContactList(ClientID,ContactNames) 
AS
(

 SELECT c1.ClientID,
        Names = SUBSTRING(( SELECT ', ' + c2.ContactName
                            FROM Contacts c2
                            WHERE c1.ClientID = c2.ClientID
                            FOR XML PATH ('')),3,8000 ))
 FROM Contacts c1
 GROUP BY c1.ClientID

 )

SELECT 
       cl.ID,
       cl.CompanyName,
       cl.Address,
       ctelist.ContactNames

FROM Clients cl
INNER JOIN CTEContactList ctelist
ON cl.ID = cteList.ClientID

Comments

0

Sounds like you need to do a table join.

Example: two tables here

1. Person
2. Orders

Query:

SELECT 
    Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders ON Persons.P_Id = Orders.P_Id
ORDER BY Persons.LastName

4 Comments

This is what I thought at first too, but the OP wants to denormalize the results and roll all the contactNames as comma-separated in a single row.
This won't work because the OP requires all contacts of the company in a single column.
What is the associated link between the two tables? Is it ClientId from Contacts and Id from Clients? Anyway to show some example data from each table?
Clientid (Contacts) = Id (Clients)
0

You didn't specify your DBMS, so I'm assuming PostgreSQL:

select string_agg(ct.contactName, ', '), cl.companyname, cl.address
from contacts ct
  join clients cl on cl.id = ct.clientId
group by cl.companyname, cl.address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.