Solved

Select with group by AND coalesce??

Posted on 2013-01-21
2
398 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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
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
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

751 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