select chepaihao,max(dzsj)-min(dzsj)/((select count(*) from dzxxtest where lineid=300 order by chepaihao)-1) from dzxxtest where lineid=300 group by chepaihao.
以上是假定一个车牌只行驶一次。
其实即使一个车牌行驶多次(中间的停站时间也包含在内),也是同样的啊。你试试看,对不对?

解决方案 »

  1.   

    select chepaihao,(max(dzsj)-min(dzsj))/((select count(*) from dzxxtest where lineid=300 group by chepaihao)-1) from dzxxtest where lineid=300 group by chepaihao.得到的结果的单位应该是天,换算诚你想要的单位。
      

  2.   

    另外,不需要用到chepaihao字段,算所有车的平均运行时间
      

  3.   

    select lineid,stationid,avg(diff_dzsj) avg_diff_dzsj
    from (
       select lineid,stationid,chepaihao,
          dzsj - nvl(lag(dzsj,1,null) over (partition by lineid,chepaihao order by station_id ),dzsj) /*nvl 在此的作用保证如果在起点站,时间差为0*/ diff_dzsj
       from DZXXTEST
       where ...
    )
    group by lineid,stationid
      

  4.   

    入骨觉得这样不行的话, 还可以考虑在子查询中将前一个stationid/后一个stationid取出来,外面根据这两个stationid分组汇总