Solved

Select with group by AND coalesce??

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

630 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