一个高难度的sql。。请指教

dd121011 2012-03-20 09:12:56
如果当天发布的输出超过5条,我想要当天发布的最早的5条,如果当天发布的数据,不够5条,我想要前一天发布的最早的来补充够五条,如果前一天发布的还不够5条,就用大前一天的数据来补充够这5条。
总的来说就5条数据。这个怎么实现,百思不得骑姐。。。求指教
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
终端用户 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dd121011 的回复:]

引用 2 楼 canhui87 的回复:
先按日期降序,再按当天时间升序来排序
select *
from
(select a.*,row_number() over(order by trunc(create_date,'yyyymmdd') desc,create_date asc) rn
from tb a
)
where rn<=5
;

没看懂,按当天时间降序是怎么……
[/Quote]
两个排序:
1,按照“年月日”的降序来排,因为楼主最想要的为当天的数据嘛,当天不够,就用前天的,所以用降序。
2,按照“时分秒”的升序来排,因为如果超过5条的话,楼主想要最早的5条,也就是当天时间最小的5条,所以“时分秒”按照升序来排
mailking 2012-03-20
  • 打赏
  • 举报
回复
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
终端用户 2012-03-20
  • 打赏
  • 举报
回复
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

在或者,使用分析函数,如楼上这位,应该都可以吧
dd121011 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 canhui87 的回复:]
先按日期降序,再按当天时间升序来排序
select *
from
(select a.*,row_number() over(order by trunc(create_date,'yyyymmdd') desc,create_date asc) rn
from tb a
)
where rn<=5
;
[/Quote]
没看懂,按当天时间降序是怎么写的啊
dd121011 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 canhui87 的回复:]
先按日期降序,再按当天时间升序来排序
select *
from
(select a.*,row_number() over(order by trunc(create_date,'yyyymmdd') desc,create_date asc) rn
from tb a
)
where rn<=5
;
[/Quote]
执行不了。。。给个能执行的吧。脑袋乱,看不懂了。

select *
from (select a.*,
row_number() over(order by trunc(create_date, 'yyyymmdd') desc, create_date asc) rn
from t_info a)
where rn <= 5;
虫洞 2012-03-20
  • 打赏
  • 举报
回复
先按日期降序,再按当天时间升序来排序
select *
from
(select a.*,row_number() over(order by trunc(create_date,'yyyymmdd') desc,create_date asc) rn
from tb a
)
where rn<=5
;
dd121011 2012-03-20
  • 打赏
  • 举报
回复
字段只需要一个info_id,和create_date就够了吧。求指教。谢谢

3,499

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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