一个表
select '001' as [id],'aaa' as [desc]
into #t1
union
select '001','bbb'
union
select '002','ccc'
union
select '002','ddd'
union
select '003','eee'
union
select '004','fff'想要形成以下形式的数据集
001 aaa,bbb
002 ccc,ddd
003 eee
004 fff
如果不考虑用function,如何做?
select '001' as [id],'aaa' as [desc]
into #t1
union
select '001','bbb'
union
select '002','ccc'
union
select '002','ddd'
union
select '003','eee'
union
select '004','fff'想要形成以下形式的数据集
001 aaa,bbb
002 ccc,ddd
003 eee
004 fff
如果不考虑用function,如何做?
解决方案 »
- 急急急--- 表结构作为左表,数据作为右表,然后做左联结
- sql作业成功后,无法查看作业历史
- 一字符串相加的问题,提示必须声明变量!! 帮助看看,急~在线
- 一条复杂的SQL语句,为什么第一次查询很慢,而第二次查询却明显快了?
- SQL Server数据库还原不成功,请各位大侠帮忙看看,着急!!@!!!!!!
- &&&&&&&&&&&&&&&&&为什么这样效率很低????????
- 百分求网站发布!
- 又见csdn!大家帮忙出出主意!求解!谢谢
- 紧急求教,sql server进行差异复制的最好办法是什么,
- 很简单的问题——如何在SQL Server的数据库中输入比如图片、MP3音乐等文件?
- sqlserver2005新建登录名的问题
- 怎样设置可以让别人访问我的SQL2005?急!在线等
select '001' as [id],'aaa' as [desc]
into #t1
union
select '001','bbb'
union
select '002','ccc'
union
select '002','ddd'
union
select '003','eee'
union
select '004','fff'create table #temp ([id] varchar(03),[desc] varchar(100))
declare @id varchar(03),@desc varchar(03)
declare c1 cursor for
select * from #t1 order by [id]
open c1
fetch next from c1 into @id,@desc
while @@fetch_status=0
begin
if exists (select 1 from #temp where [id]=@id)
begin
update #temp set [desc]=[desc]+','+@desc where [id]=@id
end
else
begin
insert into #temp ([id],[desc]) values (@id,@desc)
end
fetch next from c1 into @id,@desc
end
close c1
deallocate c1select * from #tempdrop table #t1
drop table #temp/*結果*/
id desc
---------------------
001 aaa,bbb
002 ccc,ddd
003 eee
004 fff