declare @n varchar(50) --保存分组后字段A的值 declare @MAX int --保存字段A分组后不同的总个数 declare @x int --保存相同字段A的个数 declare @i int --总个数增值 declare @y int --相同字段A个数增值 declare @sql varchar(500) --sql语句 set @sql='' set @MAX=0 set @i=0 set @y=0 select 字段A into ##表B from 表A group by 字段A order by 字段A select @MAX=count(*) from ##表B create table #G(字段A varchar(50),字段B varcahr(8000)) --结果表 while @i<=@MAX begin set @sql='select top 1 * into ##表C from ##表B ' +' where not 字段A in (select top ' +convert(varcahr(50),@i)+' 字段A from 表B order by 字段A) ' +' order by 字段A ' exec (@sql) select @n=字段A from ##表C select @x=count(*) from 表A where 字段A=@n select daintily(1,1) as id ,* into ##D from 表A where 字段A=@n declare @B varchar(8000) set @B='' set @sql='' while @y<@x begin set @sql='select top 1 * into ##F from ##D where not id in (select Top ' +@y+' id from ##D order by id) order by id' exec (@sql) select @B=字段B+' '+@B from ##F drop table ##F set @y=@y+1 end insert into #G(字段A,字段B) valuse(@n,@B) drop table ##D drop table ##表C set @i=@i+1 end select * from #G drop table ##表B
declare @MAX int --保存字段A分组后不同的总个数
declare @x int --保存相同字段A的个数
declare @i int --总个数增值
declare @y int --相同字段A个数增值
declare @sql varchar(500) --sql语句
set @sql=''
set @MAX=0
set @i=0
set @y=0
select 字段A into ##表B from 表A group by 字段A order by 字段A
select @MAX=count(*) from ##表B
create table #G(字段A varchar(50),字段B varcahr(8000)) --结果表
while @i<=@MAX
begin
set @sql='select top 1 * into ##表C from ##表B '
+' where not 字段A in (select top '
+convert(varcahr(50),@i)+' 字段A from 表B order by 字段A) '
+' order by 字段A '
exec (@sql)
select @n=字段A from ##表C
select @x=count(*) from 表A where 字段A=@n
select daintily(1,1) as id ,* into ##D from 表A where 字段A=@n
declare @B varchar(8000)
set @B=''
set @sql=''
while @y<@x
begin
set @sql='select top 1 * into ##F from ##D where not id in (select Top '
+@y+' id from ##D order by id) order by id'
exec (@sql)
select @B=字段B+' '+@B from ##F
drop table ##F
set @y=@y+1
end
insert into #G(字段A,字段B) valuse(@n,@B)
drop table ##D
drop table ##表C
set @i=@i+1
end
select * from #G
drop table ##表B
用这段sql脚本
做一些修改
也很容易得出结果.