------解决方案-------------------- select a.milleage - b.milleage as summilleage
from table1 a ,table1 b
where not exists (
select 1 from table1 where gpstime > a.gpstime
)
and not exists (
select 1 from table1 where gpstime < b.gpstime
)
------解决方案-------------------- 一楼回复的很对,中间项目向抵消,只剩最后一个和第一个的运算! ------解决方案-------------------- 怎么算出来的?能解释一下么。 ------解决方案-------------------- 数学问题
(A2-A1)+(A3-A2)+……+(An-An-1)
=-A1+(A2-A2)+(A3-A3)+……+(An-1-An-1)+An
=An-A1 ------解决方案-------------------- 29-12=17 ------解决方案-------------------- 神啊,我的问题我都写不出来了 ------解决方案-------------------- ;with cte as (
select ROW_NUMBER() OVER (ORDER BY gpstime) AS Row, abc,gpstime from Table_test
)
select sum(b.abc - a.abc)
from cte a,cte b
where a.row = b.row -1
and b.abc > a.abc
and b.abc < a.abc + 10
------解决方案-------------------- select (select top 1 abc from tb order by gpstime desc)-(select top 1 abc from tb) ------解决方案-------------------- 模拟你的数据和脚本
--建表
create table table_Test(abc int,gpstime datetime)
insert into table_Test values (12,'20130128')
insert into table_Test values (15,'20130129')
insert into table_Test values (14,'20130130')
insert into table_Test values (25,'20130131')
insert into table_Test values (29,'20130201')
--你的脚本
select sum((b.abc-a.abc )) as abc
from (select ROW_NUMBER() OVER (ORDER BY gpstime) AS Row, abc,gpstime
from Table_test )a
join (select t.Row,abc,gpstime
from (select ROW_NUMBER() OVER (ORDER BY gpstime) AS Row, abc,gpstime
from Table_test )t
where t.Row>1)b
on a.Row = b.row-1