表结构 如下  表名:test1
  字段 id , ttime ,tvalue  类型都是number类型;
   
   主键是由 id 和 ttime 联合组成;
 
   其中ttime 是 1970-01-01 08:00:00 到现在的秒数 ;    里面的数据 每个ID 每天 有好几个时间段的数据,比如 ID为1 的 1点 的数据 ,2点, 的数据,2.30 的数据 等等 
    
    现在要得到 每个ID 每天 最接近23点的记录的数据, 请问该如何实现啊?

解决方案 »

  1.   

    ttime的数据格式是怎么样的?
      

  2.   

    with tb as(select 1 id, 400000 ttime ,'dsads' tvalue from dual union all
    select 1 id, 400009 ttime ,'dsads' tvalue from dual union all
    select 2,333333 ttime,'dsads' from dual union all
    select 2,333334 ttime,'dsads' from dual union all
    select 3,733334 ttime,'dsads' from dual union all
    select 6,833334 ttime,'dsads' from dual union all
    select 6,833333 ttime,'dsads' from dual )
    select * from
    (select id ,ttime,tvalue,
    row_number()over(partition by id, trunc((ttime +8*3600)/(3600*24))--id 天分组
    order by abs(mod(ttime +8*3600,3600*24)-23*3600)asc) rn --按照绝对值升序
    from tb)
    where rn=1
    不知道是不是这样啊……
    你没给数据,自己编的……
      

  3.   

    谢谢xpingping 的回复  数据如下 :
    247 1324866653.000 150.521
    247 1324866683.000 150.966
    247 1324866713.000 151.635 248 1324866653.000 0.021
    248 1324866683.000 0.022
    248 1324866713.000 0.023当然这个是根据ID和时间排过序的, 数据库中的数据就是类似这样的数据,请问该如何实现我想要的结果呢?非常感谢
      

  4.   

    select * from
    (select id ,ttime,tvalue,
    row_number()over(partition by id, trunc((ttime +8*3600)/(3600*24))--id 天分组
    order by abs(mod(ttime +8*3600,3600*24)-23*3600)asc) rn --按照绝对值升序
    from tb)
    where rn=1
    这个查询的结果,不是你想要的吗?
    如果不是,你把你的预期值贴出来……