如果当天发布的输出超过5条,我想要当天发布的最早的5条,如果当天发布的数据,不够5条,我想要前一天发布的最早的来补充够五条,如果前一天发布的还不够5条,就用大前一天的数据来补充够这5条。
总的来说就5条数据。这个怎么实现,百思不得骑姐。。。求指教
------解决方案--------------------
先按日期降序,再按当天时间升序来排序
select *
from
(select a.*,row_number() over(order by trunc(create_date,'yyyymmdd') desc,create_date asc) rn
from tb a
)
where rn<=5
;
------解决方案--------------------
- SQL code
1,先按照“年月日”来排序,使用降序,保证当前天为第一条2,然后再每一天中,按照时间来排序,使用升序,保证最早的日期为第一条。好像就可以了吧:sql如下:select * from (select * from table1 order by trunc(create_date,'yyyyMMdd') desc,trunc(create_date,'hh24miss') asc) where rownum < 6或者select * from (select * from table1 order by to_char(create_date,'yyyyMMdd') desc,to_char(create_date,'hh24miss') asc) where rownum < 6在或者,使用分析函数,如楼上这位,应该都可以吧
------解决方案--------------------
select * from (
select info_id,create_date,rownum rn,rnb from
(select info_id,create_date, rownumber()over(partition by trunc(create_date,'yyyymmdd') order by create_date) rnb from tb) order by trunc(create_date,'yyyymmdd') desc,rnb
)
where rn<=5
------解决方案--------------------