February 27, 2012 at 8:51 am
Hello,
I have data like below
create table #Temp( ID varchar(10), Name varchar(100) )
insert into #Temp values('1','Brian')
insert into #Temp values('1','James')
insert into #Temp values('2','Jim')
insert into #Temp values('2','John')
insert into #Temp values('2','Bob')
I need write a query so that the output looks like below
1 Brain,James
2 Tim,John,Bob
I figured i can use COALESCE to get a comma separated list of names.but am struggling to group the
names by Id. Any help is greatly appreciated.
declare @TempMessage as varchar(100)
SET @TempMessage =NULL
select @TempMessage = COALESCE(@TempMessage + ',' + Name,Name)
from #Temp
February 27, 2012 at 8:55 am
Can you explain where this result comes from?
This was edited for clarity while I was posting. 😀
Btw, excellent job posting you ddl and sample data!!!
What you are describing is a cross tab, a dynamic one to be more specific. If at all possible this is a lot easier in the front end. If you need to do this in sql Jeff wrote a couple of article about this.
http://www.sqlservercentral.com/articles/T-SQL/63681/[/url]
http://www.sqlservercentral.com/articles/Crosstab/65048/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 27, 2012 at 8:58 am
It's best to let your presentation layer handle things like this. If you have to do it in the database layer, you can use FOR XML to concatenate the results. Just search for "concatenate with FOR XML" and you should find what you require.
John
February 27, 2012 at 9:05 am
I have used for xml path to do a similar thing before.
;with SampleDataR as
(
select *, ROW_NUMBER() over (partition by ID order by Name) rownum
from #Temp
)
select distinct ID,(
select Name
+ case when s1.rownum = (select MAX(rownum) from SampleDataR where ID = s1.ID)
then '' else ',' end from SampleDataR s1
where s1.ID = s2.ID
for xml path(''),type).value('(.)[1]','varchar(max)') CSVNames
from SampleDataR s2
February 27, 2012 at 9:07 am
/facepalm
Thanks guys don't know why I didn't think of for xml. :hehe:
Guess I need more coffee this morning.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 27, 2012 at 9:23 am
Thanks for the reply Guyz. you are the best 🙂
March 4, 2012 at 6:47 pm
John Mitchell-245523 (2/27/2012)
It's best to let your presentation layer handle things like this.
I'll say "It Depends"... especially if there's no presentation layer that can handle it. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply