Declare @tb TABLE (Id int IDENTITY,fdate datetime,fzqdm bigint,fkmdm varchar(50),facctname varchar(50),facctattr varchar(50),facctattrid varchar(10),fzqsl numeric(18,4),fzqsz numeric(18,facctlevel varchar(10),fby varchar(10),fcurcode varchar(10)) insert into @tb(fdate,fzqdm,fkmdm,facctname,facctattr,facctattrid,fzqsl,fzqsz,facctlevel,fby,fcurcode) select fdate,fcurcode from table1 select count(*) from @tb where id<=t.id and fzqdm=t.fzqdm and facctattrid = t.facctattrid and fdate=t.fdate and fby=t.fby and fcurcode=t.fcurcode),t.* from @tb t 红色代码是不可少的,绿色部分相当于row_numbers()函数中的partition by部分。