select ID, (select top 1 Value from 表 b where b.datetime=max(a.datetime) and b.id=a.id)- (select top 1 Value from 表 b where b.datetime=min(a.datetime) and b.id=a.id) from 表 a group by id
select top 2 id,rtrim(convert(char,datetime,112)) as datetime,value=(select min(value) from a where a.id=t.id and a.datetime>t.datetime )-value from a t
(select top 1 Value from 表 b where b.datetime=max(a.datetime) and b.id=a.id)- (select top 1 Value from 表 b where b.datetime=min(a.datetime) and b.id=a.id)
from 表 a
group by id
from a t