Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SubQuery as Comma-Delimited List

Posted on 1998-09-17
3
Medium Priority
?
342 Views
Last Modified: 2012-05-04
Hello,

Is there a way to reduce a result set by putting the results of a sub query in a single column?  

For example, instead of:
Project Meeting                             Monday
Project Meeting                             Tuesday
Conference Call                             Tuesday
Conference Call                             Thursday
Conference Call                             Friday

get:
Project Meeting                             Monday, Tuesday
Conference Call                            Tuesday, Thursday, Friday

Is this possible?
0
Comment
Question by:jreilly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Victor Spiridonov earned 400 total points
ID: 1090155
It is not possible to do using just SQL ,however you can write a stored procedure, and use cursor to get this result.
0
 

Author Comment

by:jreilly
ID: 1090156
Do you have some quick code for this?

Thanks.
0
 
LVL 7

Expert Comment

by:Victor Spiridonov
ID: 1090157
Let's suppose table is t1,columns task and day:
Create procedure test
as
declare
@day_list  varchar(255)
@task varchar(50)
@task_prev varchar(50)

declare c_1 cursor for
select task,day
order by task

create #temp_table_1 (task varchar(50),day_list varchar(255))
open_c1
fetch next from c_1 into @task,@day
while @@fetch_status<>-1
 BEGIN
  IF (@@fetch_status <> -2)
   BEGIN      
     if (@task_prev="")
       begin
       @task_prev=@task
        @day_list=@day
     else
       if @task_prev=@task
         @day_list=@day_list+","+@day
       else
          insert into #temp_table_1   values(@task_prev,@day_list)
         @day_list=@day
    @task_prev=@task
   end
   end
fetch next from c_1 into @task,@day
END
deallocate c_1
select * from #temp_table_1


0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question