Solved

SubQuery as Comma-Delimited List

Posted on 1998-09-17
3
332 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now