Solved

SQL Query Syntax question

Posted on 2013-11-25
3
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 11

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

635 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