jreilly
asked on
SubQuery as Comma-Delimited List
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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+","+@d ay
else
insert into #temp_table_1 values(@task_prev,@day_lis t)
@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
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+","+@d
else
insert into #temp_table_1 values(@task_prev,@day_lis
@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
ASKER
Thanks.