select 0 排号,计量性质,发货单位,物质名称 into #临时表 from 你的表 order by 计量性质,物质名称
declare @a int,@b varchar(100),@c varchar(100)
update #临时表 set @a=case when 计量性质=@b and 物质名称=@c then @a when 计量性质=@b and 物质名称<>@c then @a+1 else 1 end,@b=计量性质,@c=物质名称,排号=@aselect * from #临时表
go
drop table #临时表

解决方案 »

  1.   

    如果不用#临时表呢,因为我要把它写到delphi中,怎么做了??
      

  2.   

    不用临时应该不行,写到delphi中没问题啊.你将它改成存储过程就行了.create proc p_show
    as
    set nocount on
    select 0 排号,计量性质,发货单位,物质名称 into #临时表 from 你的表 order by 计量性质,物质名称
    declare @a int,@b varchar(100),@c varchar(100)
    update #临时表 set @a=case when 计量性质=@b and 物质名称=@c then @a when 计量性质=@b and 物质名称<>@c then @a+1 else 1 end,@b=计量性质,@c=物质名称,排号=@a
    select * from #临时表
    set nocount off
    go
    然后调用就行了:
    exec p_show