sql 求差值

有一组数据,这组数据是不断增加的,想求每小时的差值,规则是:本小时差值=后一小时第一个数据减去本小时第一个数据,中间数据可以忽略不管
如下表
DT            Value
2008-1-1 1:01 23
2008-1-1 1:32 25
2008-1-1 1:59 27
2008-1-1 2:06 28
2008-1-1 2:46 29
2008-1-1 3:06 37
2008-1-1 3:26 41
2008-1-1 3:56 42
2008-1-1 4:00 42

结果为
DT          Value
2008-1-1 1时 5      (28-23)
2008-1-1 2时 9      (37- 28)
2008-1-1 3时 5      (42- 37) 这个问题第1个回答:

DT            Value
这个问题第2个回答:
<!--



Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



-->

select convert(char(10),c.dt,120),a.value - b.value as value

from (

select * from ta a where not exists(select 1 from ta where convert(char(13),a.dt,120) = convert(char(13),dt,120) and dt > a.dt)

) c

left join (

select * from ta a where not exists(select 1 from ta where convert(char(13),a.dt,120) = convert(char(13),dt,120) and dt < a.dt) b

on convert(char(13),c.dt,120)= convert(char(13),b.dt,120)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值