请教各位,我想查询(比如,字段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 t
where 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 ...)t2
where t2.rn <=10