Consider the following DDLS (which CREATEs 2 tables):
CREATE TABLE Contacts (ContactID int,ContactName varchar(50))
INSERT INTO Contacts VALUES(1,'Danny')
INSERT INTO Contacts VALUES(2,'John')
INSERT INTO Contacts VALUES(3,'Chris')
INSERT INTO Contacts VALUES(4,'Dolly')
INSERT INTO Contacts VALUES(5,'Mary')
------------------------------------------------------------
CREATE TABLE Client(ClientID int,ContactID int,ClientName varchar(50))
INSERT INTO Client VALUES(1,1,'Business Academy')
INSERT INTO Client VALUES(2,1,'Pri. School')
INSERT INTO Client VALUES(3,2,'Sec. School')
INSERT INTO Client VALUES(4,2,'Junior College')
INSERT INTO Client VALUES(5,3,'Senior College')
INSERT INTO Client VALUES(6,4,'Community Education')
INSERT INTO Client VALUES(7,5,'Youth Development')
------------------------------------------------------------
Next I have the following SQL query:
SELECT DISTINCT Contacts.ContactID,Contacts.ContactName,Client.ClientName FROM Contacts INNER JOIN Client ON
Client.ContactID=Contacts.ContactID
When the above query is executed in the Query Analyzer, 7 records are displayed. Please have a look at the recordset. You will find that under the column 'ContactName', the record 'Danny' appears twice (though I have used DISTINCT) since it has 2 ClientNames ('Business Academy' & 'Primary School') related to it. But I want that the record 'Danny' should be displayed only once in the recordset & under the column named 'ClientNames', the corresponding record should be a comma-delimited list i.e. I want that under the column 'ClientNames', the corresponding record should be displayed as Business Academy, Primary School. The records should look something like this:
How do I display the records like I have shown above?
Thanks,
Arpan
CREATE TABLE Contacts (ContactID int,ContactName varchar(50))
INSERT INTO Contacts VALUES(1,'Danny')
INSERT INTO Contacts VALUES(2,'John')
INSERT INTO Contacts VALUES(3,'Chris')
INSERT INTO Contacts VALUES(4,'Dolly')
INSERT INTO Contacts VALUES(5,'Mary')
------------------------------------------------------------
CREATE TABLE Client(ClientID int,ContactID int,ClientName varchar(50))
INSERT INTO Client VALUES(1,1,'Business Academy')
INSERT INTO Client VALUES(2,1,'Pri. School')
INSERT INTO Client VALUES(3,2,'Sec. School')
INSERT INTO Client VALUES(4,2,'Junior College')
INSERT INTO Client VALUES(5,3,'Senior College')
INSERT INTO Client VALUES(6,4,'Community Education')
INSERT INTO Client VALUES(7,5,'Youth Development')
------------------------------------------------------------
Next I have the following SQL query:
SELECT DISTINCT Contacts.ContactID,Contacts.ContactName,Client.ClientName FROM Contacts INNER JOIN Client ON
Client.ContactID=Contacts.ContactID
When the above query is executed in the Query Analyzer, 7 records are displayed. Please have a look at the recordset. You will find that under the column 'ContactName', the record 'Danny' appears twice (though I have used DISTINCT) since it has 2 ClientNames ('Business Academy' & 'Primary School') related to it. But I want that the record 'Danny' should be displayed only once in the recordset & under the column named 'ClientNames', the corresponding record should be a comma-delimited list i.e. I want that under the column 'ClientNames', the corresponding record should be displayed as Business Academy, Primary School. The records should look something like this:
Code:
-----------------------------------------------------
ContactID ContactName ClientName
-----------------------------------------------------
1 Danny Business Academy,Pri. School
2 John Junior College
..............................
..............................
-----------------------------------------------------
Thanks,
Arpan