Solved

SQL Query Syntax question

Posted on 2013-11-25
3
158 Views
Last Modified: 2013-12-05
Hello all,

I am trying to do the following.   I have a table called Customer and then I have a table called CustomerGroup 1 to Many to Customer.   The fields for example are as follows:

Table:   Customer
Fields:  CustId,  CustName, CustAddress

Table: CustomerGroup
Fields:  CustId, GroupName

I want to get all the fields from table Customer then I want a comma seperated string of all the GroupNames from CustomerGroup stripped off end comma.   How can I do this?  Sample data:

Customer
CustID 1
CustName ABC
CustAddress 12 Water St

CustomerGroup
CustID 1
GroupName A

CustID 2
GroupName B

CustID 3
GroupName C

Record would return
1,  ABC,  12 Water St, 'A,B,C'
0
Comment
Question by:sbornstein2
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 245 total points
ID: 39676032
SELECT C.CustID, C.CustName, C.CustAddress,
      STUFF(
      (
            SELECT ', ' + CG.GroupName
            FROM CustomerGroup AS CG
            WHERE CG.CustID = C.CustID
            FOR XML PATH('')
      ), 1, 2, '') AS CustomerGroups
FROM Customer AS C
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 39676598
Try
SELECT distinct CustID,CustName,CustAddress,
       stuff( (SELECT ', ' + GroupName
               FROM CustomerGroup
               ORDER BY 1
               FOR XML PATH('')),1 ,2 ,'')
       AS column4
 from Customer
order by 1,2;

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 39700164
tx
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now