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
 Old Forums
 CLOSED - General SQL Server
 Group By + Append Description

Author  Topic 

matt_calhoon
Posting Yak Master

235 Posts

Posted - 2006-09-26 : 04:31:06
Hi there,
I have a table with 2 fields. CATEGORY and DESCRIPTION. IF I run a query:

Select CATEGORYID, DESCRIPTION from mytable

it will return:

33, 'test'
33, 'another test'
33, '3rd test'
34, 'blah blah'
34, 'etc etc'

is it possible to write a query which will group the results (by the categoryid) but append the data in the description. i.e.return something like

33, 'test<br>another test<br>3rd test'
34, 'blah blah<br>etc etc'

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-26 : 04:32:39
have a look at this article..

http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx

Chirag
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-26 : 08:33:29
Note that if the concatenated string exceeds 8000 characters, you have to use more than one variable. If you use front end application, do the concatenation there

Madhivanan

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

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-09-26 : 09:15:35
Sounds like you are trying to format your data with SQL. My advice is to return the raw data as is and let your client application (in this case, a web page I am guessing) do the formatting. It is often much easier and more efficient to do it that way.

Let us know which web application you are using (i.e., ASP.NET ?) and how you are trying to display the data and we should be able to help you. Also, as Madhivanan says, the other problem with doing this in T-SQL is that you run into the 8000 varchar limit.

- Jeff
Go to Top of Page
   

- Advertisement -