Oracle区间查找问题?

查询规定时间内每天2点-6点,6-10,14-18,18-22,22-次日2点,区间中时间最大的值,就比如我现在2-6点数据有,3点4点2条,我只取4点这条,

 

可以用union实现你的要求,分段查询,union连接就可以了。

select * from (select * 表名 where jlsj between '2021-5-14 02:00:00' and '2021-5-14 05:59:59' order by jlsj desc) where rownum=1
union all
select * from (select * 表名 where jlsj between '2021-5-14 06:00:00' and '2021-5-14 09:59:59' order by jlsj desc) where rownum=1
union all
select * from (select * 表名 where jlsj between '2021-5-14 14:00:00' and '2021-5-14 17:59:59' order by jlsj desc) where rownum=1
union all
select * from (select * 表名 where jlsj between '2021-5-14 18:00:00' and '2021-5-14 21:59:59' order by jlsj desc) where rownum=1
union all
select * from (select * 表名 where jlsj between '2021-5-14 22:00:00' and '2021-5-14 23:59:59' order by jlsj desc) where rownum=1

 

根据时间区间找JLXH序列倒序排序通过rownum限制只取一条。

with tb as
(
select 'a' a, 97 b from dual where 时间段>= and 时间段<= and rownum<=1 order by JLXH desc  union all
select 'b' a, 96 b from dual union all
select 'a' a, 95 b from dual union all
select 'c' a, 94 b from dual union all
select 'a' a, 83 b from dual union all
select 'd' a, 87 b from dual union all
select 'c' a, 67 b from dual union all
select 'a' a, 57 b from dual
)
select a, floor(b/10)*10 || '-' || (floor(b/10)+1)*10
--       ,b
       ,count(*)
  from tb where 时间段>= and 时间段 <=
----先把每个时间段最后一条数据取出来当然上面的时间段进行处理,取小时出来判断  大概就是这个样子,具体的需要自己改改

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632