1 回答
TA贡献1877条经验 获得超1个赞
create table test5
(
[type] int not null,
[value] int not null,
[time] time
)
go
INSERT 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')
go
with 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]
添加回答
举报