SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL code to group data into comma delimited column


T-SQL code to group data into comma delimited column

Author
Message
sharonrao123
sharonrao123
SSC-Addicted
SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)

Group: General Forum Members
Points: 482 Visits: 205
hi All,

I have a view v_Newsletter with has two columns email and newsletter. There are some emails which has subscribed to more than one newsletter. Please see below for example data

Email Newsletter
Email1 Newsletter1
Email1 Newsletter2
Email2 Newsletter1
Email3 Newsletter1
Email3 Newsletter2
Email 4 Newsletter2

Now is it possible to create an excel file with two columns email and newsletter with email address occuring only once and newsletter as comma delimited field for multiple newsletter. Like below format

Email Newsletter
Email1 Newsletter1,Newsletter2
Email2 Newsletter1
Email3 Newsletter1, Newsletter2
Email4 Newsletter2

Please advice on how to proceed with the T-SQL code.

Thanks, Shilpa.
Lowell
Lowell
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123576 Visits: 41464
ugg...i was pasting an example that uses the FOR XML to resolve this, and realized you posted in a SQL 2000 forum.

the best way is to use the trick that the FOR XML command can do for you; even for large lists, is pretty efficient.
i'm pretty sure it still works, i changed my 2005 database to compatibility level 80, and it worked just fine, but you'll need to test it:

here's typical results with the values i used...i was too lazy to change every data element to your examples.

Resource_Id Skills
------------------------------ -----------------------
Email1 oracle,sqlserver,vb.net
Email2 excel,java,sqlserver
Email3 java,oracle,vb.net




declare @skills table (Resource_Id varchar(30), Skill_Id varchar(20))
insert into @skills
select 'Email1' , 'sqlserver' union all
select 'Email1' , 'vb.net' union all
select 'Email1' , 'oracle' union all
select 'Email2', 'sqlserver' union all
select 'Email2', 'java' union all
select 'Email2', 'excel' union all
select 'Email3', 'vb.net' union all
select 'Email3', 'java' union all
select 'Email3', 'oracle'
---
select * from @skills s1
--- Concatenated Format
set statistics time on;
SELECT Resource_Id,stuff(( SELECT ',' + Skill_Id
FROM @skills s2
WHERE s2.Resource_Id= s1.resource_ID --- must match GROUP BY below
ORDER BY Skill_Id
FOR XML PATH('')
),1,1,'') as [Skills]
FROM @skills s1
GROUP BY s1.Resource_Id --- without GROUP BY multiple rows are returned
ORDER BY s1.Resource_Id
set statistics time off;



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Garadin
Garadin
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11033 Visits: 4107
i'm pretty sure it still works, i changed my 2005 database to compatibility level 80, and it worked just fine, but you'll need to test it:


Alas, it does not :/. This trick is 2005+. There really weren't any particularly good ways to do it in SQL 2000.

Here's a good post on the different ways this is normally accomplished, you can pick your poison.

http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
Garadin
Garadin
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11033 Visits: 4107
Edit: Silly late night forum lag.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
sharonrao123
sharonrao123
SSC-Addicted
SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)

Group: General Forum Members
Points: 482 Visits: 205
Thanks for your suggestion the "stuff" worked like a charm.
sharonrao123
sharonrao123
SSC-Addicted
SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)SSC-Addicted (482 reputation)

Group: General Forum Members
Points: 482 Visits: 205
Thanks for your suggestion the "stuff" worked like a charm. You are a star
Garadin
Garadin
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11033 Visits: 4107
sharonrao123 (11/24/2009)
Thanks for your suggestion the "stuff" worked like a charm. You are a star


Just so that people reading this later don't get the wrong idea, can you confirm the version of SQL server you are using? (SELECT @@VERSION if you're not sure) This method does not work on SQL 2000.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
pcasey
pcasey
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 113
I found your post by using a search engine. It is just what I needed and easy to understand. Thank you SO MUCH.:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search