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 2005 Forums
 Transact-SQL (2005)
 Grouping with strings

Author  Topic 

sqlfan2

26 Posts

Posted - 2007-10-31 : 14:11:05
Is there a way that I can group records but combine strings from a column in a concatenation (perhaps with a sperator):

This works:
select PairID, count(Name)
from OJ
group by PairID
order by PairID;

But I want something like this:
select PairID, concat(Name,",")
from OJ
group by PairID
order by PairID;

----------------------------------
Standing on the shoulders of giants

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-31 : 21:31:42
write a UDF to do that. see http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sqlfan2

26 Posts

Posted - 2007-11-01 : 00:03:15
That was really helpful - thanks - gave me all the info I needed to solve the problem.

----------------------------------
Standing on the shoulders of giants
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-01 : 02:28:26
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sqlfan2

26 Posts

Posted - 2007-11-01 : 11:11:50
Great link/resource - thanks.

----------------------------------
Standing on the shoulders of giants
Go to Top of Page
   

- Advertisement -