急 请教 oracle 分组 查询

getdate 2012-03-13 12:15:21
表结构 如下

表名:test1
字段 id , ttime ,tvalue 类型都是number类型;

主键是由 id 和 ttime 联合组成;

其中ttime 是 1970-01-01 08:00:00 到现在的秒数 ;

里面的数据 每个ID 每天 有好几个时间段的数据,比如 ID为1 的 1点 的数据 ,2点, 的数据,2.30 的数据 等等

现在要得到 每个ID 每天 最接近23点的记录的数据, 请问该如何实现啊?

...全文
146 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
getdate 2012-03-23
  • 打赏
  • 举报
回复
虽然用上面的方法没有解决我的问题,但还是要感谢两位的回复
getdate 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yinan9 的回复:]
ttime的数据格式是怎么样的?
[/Quote]

1331481600.000 类似这样的数据格式,
yinan9 2012-03-13
  • 打赏
  • 举报
回复
ttime的数据格式是怎么样的?
getdate 2012-03-13
  • 打赏
  • 举报
回复
自己先顶下吧~
xpingping 2012-03-13
  • 打赏
  • 举报
回复
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

这个查询的结果,不是你想要的吗?
如果不是,你把你的预期值贴出来……
getdate 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xpingping 的回复:]
SQL code
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
se……
[/Quote]


谢谢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和时间排过序的, 数据库中的数据就是类似这样的数据,请问该如何实现我想要的结果呢?非常感谢
xpingping 2012-03-13
  • 打赏
  • 举报
回复
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

不知道是不是这样啊……
你没给数据,自己编的……

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧