当前位置: 代码迷 >> Oracle管理 >> ,小弟我想查询(比如,字段create_date) 2011-12-09 7点到8点的前10条 和 9点到10点的前10条 等等,这个SQL如何写
  详细解决方案

,小弟我想查询(比如,字段create_date) 2011-12-09 7点到8点的前10条 和 9点到10点的前10条 等等,这个SQL如何写

热度:324   发布时间:2016-04-24 05:39:52.0
请教各位,我想查询(比如,字段create_date) 2011-12-09 7点到8点的前10条 和 9点到10点的前10条 等等,这个SQL怎么写?
请教各位,如果我想查询一个时间字段的 2011-12-09 号的 7点到8点的前10条 、 9点到10点的前10条 、11点到12点的前10条 等等,总共30条,这个SQL咋写?

------解决方案--------------------
select t.*,t.rownum from tablename t where time >= '2011-12-09 07%' and time <= '2011-12-09 08%' and t.rownum<=10
UNION ALL
select t.*,t.rownum from tablename t where time >= '2011-12-09 09%' and time <= '2011-12-09 10%' and t.rownum<=10
UNION ALL
select t.*,t.rownum from tablename t where time >= '2011-12-09 11%' and time <= '2011-12-09 12%' and t.rownum<=10

------解决方案--------------------
SQL code
select * from (select row_number() over (partition by trunc(time_data,'hh') order by time_data) rn,t.* from twhere sign(time_data-to_date('2009-12-09 07','yyyy-mm-dd hh24'))+sign(time_data-to_date('2009-12-09 08','yyyy-mm-dd hh24'))=0 or sign(time_data-to_date('2009-12-09 09','yyyy-mm-dd hh24'))+sign(time_data-to_date('2009-12-09 10','yyyy-mm-dd hh24'))=0 or ...)t2where t2.rn <=10
  相关解决方案