Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query Syntax question

Posted on 2013-11-25
3
Medium Priority
?
166 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 980 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

660 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