create table dbo.ss(a int,b int,c int,d int)
go
insert into ss
select 2 , 10 , 10 , 20 union all
select 5 , 20 , -10 , 10 union all
select 100 , 11 , 1 , 12 union all
select 120 , 12 , 5 , 17 union all
select 121 , 17 , 2 , 19 union all
select 123 , 23 , -10 , 13 union all
select 130 , 13 , 1 , 14 union all
select 153 , 14 , 2 ,16
go
select IDENTITY(smallint, 100, 1) as e, * into dbo.s from ss
go
select a,b,c,d from(
select s.*,s0.b as d1
from s inner join s as s0 on s.e+1=s0.e-- and s.d<>s0.d
) as t where d<>d1
go
drop table dbo.s
drop table dbo.ss
go
select 序号,异动前数量,异动后数量,isnull((select top 1 异动后数量 from a where 序号<c.序号 order by 序号 desc),0) 前行数量 from a as c where isnull((select top 1 异动后数量 from a where 序号<c.序号 order by 序号 desc),0)<>异动前数量