冉冉说
create table test5([type] int not null,[value] int not null,[time] time)goINSERT INTO test5 VALUES(2,5,'2:42:00')INSERT INTO test5 VALUES(4,-42,'3:42:00')INSERT INTO test5 VALUES(2,2,'4:42:00')INSERT INTO test5 VALUES(2,7,'2:52:00')INSERT INTO test5 VALUES(3,16,'2:42:00')INSERT INTO test5 VALUES(3,20,'3:42:00')gowith v1 as (select * from (select top 100 *, row_number() over ( partition by [type] order by time) as [rank] from test5) V1 where V1.rank=1),v2 as (select * from (select top 100 *, row_number() over ( partition by [type] order by time) as [rank] from test5) V1 where V1.rank=2)select v1.[type],v1.[value]-v2.[value] from v1 left join v2 on v1.[type]=v2.[type]