SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Group, then Concatinate
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rbh123456789
Starting Member

15 Posts

Posted - 05/31/2012 :  11:18:40  Show Profile  Reply with Quote
Hey guys, i have looked through many posts, but i haven't really seen an answer to this type of question.

I have a View below, with 2 fields.
The EventID has multiple entries since there are multiple Attendees.

EventID Attendee
13350 John Hart
13350 David Smith
13350 Jane Doe
13353 John Hart
13353 David Smith
13353 Jane Doe
13357 John Hart
13357 David Smith
13357 Jane Doe
13360 John Hart
13360 David Smith
13360 Jane Doe

I was the output to be like below.
I thought I could GROUP BY the EventID, but thats only half of what i need.
Keep in mind that there are actually hundreds of names in the tables.

EventID Attendee
13350 John Hart,David Smith,Jane Doe
13353 John Hart,David Smith,Jane Doe
13357 John Hart,David Smith,Jane Doe
13360 John Hart,David Smith,Jane Doe

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 05/31/2012 :  11:25:12  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
see
http://www.nigelrivett.net/SQLTsql/CSVStringSQL.html

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 05/31/2012 :  16:11:53  Show Profile  Reply with Quote
whats the basis on which you want to order values within comma separated list? do you've another column for this?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 06/14/2012 :  07:44:13  Show Profile  Reply with Quote
You can do a little variation of a Pivot for this one as follows:


--Creating Table

Create Table Ex
(EventID int,
 Attendee Varchar(30) )


--Inserting Sample Data

Insert Into Ex
Select 13350,	'John Hart'
Union ALL
Select 13350,	'David Smith'
Union ALL
Select 13350,	'Jane Doe'
Union ALL
Select 13353,	'John Hart'
Union ALL
Select 13353,	'David Smith'
Union ALL
Select 13353,	'Jane Doe'
Union ALL
Select 13357,	'John Hart'
Union ALL
Select 13357,	'David Smith'
Union ALL
Select 13357,	'Jane Doe'
Union ALL
Select 13360,	'John Hart'
Union ALL
Select 13360,	'David Smith'
Union ALL
Select 13360,	'Jane Doe'


--Static Pivot

Select EventID, [Name1] + ', ' + [Name2] + ', ' + [Name3] As Attendees From
	(Select *, 'Name' + Cast(ROW_NUMBER() Over (Partition By EventId Order By (Select NULL) ) As Varchar(10) ) As rn From Ex) As a
Pivot
(Max(Attendee) For rn IN ([Name1], [Name2], [Name3]) ) As Pvt


--Dynamic Pivot

Declare @cols varchar(max), @sql varchar(max), @cols1 varchar(max)
Declare @temp Table(Cols varchar(10) )
Insert Into @temp
Select Distinct rn From
	(Select *, 'Name' + Cast(ROW_NUMBER() Over (Partition By EventId Order By (Select NULL) ) As Varchar(10) ) As rn From Ex) As a
Select @cols = Coalesce(@cols + ', ', '') + QUOTENAME(Cols) From @temp
Select @cols1 = Coalesce(@cols1 + '+'', ''+', '') + QUOTENAME(Cols) From @temp
Set @sql = 'Select EventID, '+@cols1+' As Attendees From
				(Select *, ''Name'' + Cast(ROW_NUMBER() Over (Partition By EventId Order By (Select NULL) ) As Varchar(10) ) As rn From Ex) As a
			Pivot
			(Max(Attendee) For rn IN ('+@cols+') ) As Pvt'
Execute (@sql)


N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000