Solved

Select with group by AND coalesce??

Posted on 2013-01-21
2
387 Views
Last Modified: 2013-01-21
I have a table with the following data

username          Client
lrbrister            Franklin Insurance
lrbrister            Grant Insurance
gmello              Zuereg Insurance
lrbrister            Harrison Insurance
gmello              Yammo Insurance



I'm trying to do a select so I get this. (There will be upwards of 50 usernames)

username          Client
lrbrister            Franklin Insurance|Grant Insurance|Harrison Insurance
gmello              Yammo Insurance|Zuereg Insurance
0
Comment
Question by:lrbrister
2 Comments
 
LVL 4

Accepted Solution

by:
krtyknmsql earned 500 total points
ID: 38801049
You need to query something like below.
SELECT 
	UserName,
	STUFF((SELECT '|' + Client FROM CSV AS B
	 WHERE A.UserName = B.UserName
	 FOR XML PATH ('')),1,1,'') Clients
FROM CSV AS A
GROUP BY UserName

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 38801093
Thanks.
This is my final solution


; WITH cte (AE,Client) AS
(
      SELECT CVT.[Account Executive] AE,
                  C.[Client Name] Client
      FROM dbo.ClientVisitTracking CVT
      LEFT JOIN Client C ON CVT.[Client ID] = C.[Client ID]
      GROUP BY cvt.[Account Executive], C.[Client Name]

)
SELECT AE,
      STUFF((SELECT '|' + Client FROM cte AS B
       WHERE A.AE = B.AE
       FOR XML PATH ('')),1,1,'') Clients
FROM cte AS A
GROUP BY AE
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

28 Experts available now in Live!

Get 1:1 Help Now