Solved

SubQuery as Comma-Delimited List

Posted on 1998-09-17
3
335 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
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
spiridonov earned 100 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: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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 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