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

x
?
Solved

Select with group by AND coalesce??

Posted on 2013-01-21
2
Medium Priority
?
407 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
[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
2 Comments
 
LVL 4

Accepted Solution

by:
krtyknmsql earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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