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
 General SQL Server Forums
 New to SQL Server Programming
 How to concatenate fields and group them?

Author  Topic 

cgatlan
Starting Member

3 Posts

Posted - 2011-11-30 : 21:39:42
Hi Guys,

My first visit here, look like an impressive resource.
I tried a search first but couldn't find the answer I need.

The problem I have is trying to concatenate a field and group by another field. Let me give you an example..

The table in the database looks like: (over 6000 entries)

Server | Service
-------------------
sev001 | hosting
sev001 | email
sev001 | info
sev002 | info
Sev002 | email
Sev003 | print
Sev003 | File
Sev003 | hosting
Sev003 | misc
Sev003 | scan
Sev004 | email
Sev004 | misc

The output I want to archive is like this:
Sev001 | hosting, email, info
Sev002 | info, email
Sev003 | print, file, hosting, misc, scan
Sev004 | email, misc

etc..

A colleague mentioned something about 'cursor' and 'stored procedures' but doesn't know how, and I don't have the first clue where to start with those. I'm fairly new to SQL and have only been writing some basic select queries.

Can anyone help?

Thanks,

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-11-30 : 21:44:03
use the method describe here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

also read this if you are not using SQL 2005/2008 and can't use the FOR XML method
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

cgatlan
Starting Member

3 Posts

Posted - 2011-11-30 : 22:37:40
Thanks for replying KH,

Unfortunately, I'm still in the dark after viewing these links,
They are referring to an XML path.. I don't have any 'external' files, the data I'm referring to is in a table in SQL Server 2008 R2.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-11-30 : 23:18:40
quote:
Originally posted by cgatlan

Thanks for replying KH,

Unfortunately, I'm still in the dark after viewing these links,
They are referring to an XML path.. I don't have any 'external' files, the data I'm referring to is in a table in SQL Server 2008 R2.




it does not need "external files". Follow the method & syntax used in the link and you should get the result


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

Go to Top of Page
   

- Advertisement -