a 1
b 2
b 3
c 4
c 5
c 6
请用SQL变为
a 1
b 23
c 456
请问怎么实现?
假设表名是MyTable 字段是ID(varchar(4))(a,b...) Num(varchar(4))(1,2....)
create view view1//排续ID
as
select ID
from MyTable
group by ID
order by asc
go
create procedure MyProcedure//开始生成了
as
declare @count int
select @count=count(ID)
from MyTable
group by ID
declare @i int
set @i = 1
while @i <= @count
begin
declare @ch_ID varchar(4)
select top 1 @ch_ID = ID
from execute('select top '+convert(varchar(4),@i)+' ID from view1')
order by ID desc
declare @Nums varchar(256)
set @Nums = ''
select @Nums=@Nums + Num
from MyTable
where ID = @ch_ID
select @ch_ID,@Nums into #TempTable
set @i = @i + 1
end
select * from #TempTable
go
基本思路就应该这样,不过偶没调试,不知道编译能否通过
不好意思,符号打错了
改过来了
[此贴子已经被作者于2006-12-7 17:21:43编辑过]