数据表中有8条数据,每条都包含一个时间类型的数据,要求这8个时间排序后两两相减再求和,
sum((时间2-时间1) + ((时间4-时间3) + ((时间6-时间5) + ((时间8-时间7)),可否一个sql求出结果?
[code="sql"]
with temp as (
select to_date('2012-02-03 10:51:00','yyyy-MM-dd hh24:mi:ss') as time from dual
union
select to_date('2012-02-03 10:56:00','yyyy-MM-dd hh24:mi:ss') as time from dual
union
select to_date('2012-02-03 11:23:00','yyyy-MM-dd hh24:mi:ss') as time from dual
union
select to_date('2012-02-03 12:51:00','yyyy-MM-dd hh24:mi:ss') as time from dual
union
select to_date('2012-02-03 14:51:00','yyyy-MM-dd hh24:mi:ss') as time from dual
union
select to_date('2012-02-03 16:51:00','yyyy-MM-dd hh24:mi:ss') as time from dual
)
select sum(power(-1,rownum)*(time-sysdate)*24*60*60) from (select * from temp order by time) a
[/code]
这你都还不明白!
把-1的一次方、二次方、三次方、n次方算出来
再乘以记录的时间time-sysdate,这里用sysdate是为了简单,用什么都一样(当然你的行数必须是偶数)
这里的结果累加就是一共相差的秒数
sum((-1)^n*time)
不知道明白我的意思不
select sum(power(-1,rownum)*time) from (select time from table order by time)
我明白楼上的意了。
这个是oracle的语句。
我想麻烦了。想列变行,再相减。