Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Aggregate Concatenation query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-08-26 : 11:31:10
Patrick writes "Hello,

‘ My name is Patrick and for a couple of days I have been tring to figure out how to produce an aggregate concatenation query. I came up with our page (I like the site, and very usefull). I am tring to do the following



Table of data:



ControlDetailID int (FK)

LookupValue nvarchar(50)





Exp:

ControlDetailID LookupValue



1 pat

1 trisha

2 Smith

3 Jones





I am trying to get it to look like this



ControlDetailID LookupValue

1 Pat, trisha

2 Smith, Jones





Please help



Thanks



Patrick Dunn "

joshb
Yak Posting Veteran

52 Posts

Posted - 2002-08-26 : 11:41:12
A while back I was trying to do something very similar. I found these articles very useful. they should server as a good starting point.

[url]http://www.sqlteam.com/item.asp?ItemID=2368[/url] (I really like this one!)

[url]http://www.sqlteam.com/item.asp?ItemID=256[/url]

Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-08-26 : 11:41:13

create table #workingtable (
ControlDetailID int not null,
LookupValue nvarchar(50) not null,
List nvarchar(4000) null )

insert into #workingtable (ControlDetailID, LookupValue)
select
ControlDetailID,
LookupValue
from
Patricks_Table
order by
ControlDetailID,
LookupValue

declare
@list nvarchar(4000),
@lastID int

select
@list = '',
@lastID = ''

update
#workingtable
set
@list = list = case
when @lastid <> ControlDetailID then LookupValue
else @list + ', ' + LookupValue
end,
@lastid = ControlDetailID

select
ControlDetailID,
max(List) as CSV
from
#workingtable
group by
ControlDetailID

 


Jay White
{0}
Go to Top of Page
   

- Advertisement -