ORACLE SQL小时范围

时间:2019-05-28 16:02:18

标签: sql oracle plsql date-range

我有问题,我的桌子有3列

import numpy as np
for col in list(df):
    print(col)
    print(np.sort(df[col].unique()))

我需要进行验证以确保不重叠小时范围。
例如:

- date - varchar2   - varchar2 -   
| date | start_hour | end_hour |  

假设3行的日期相同。
我需要的是,这些范围不要重叠
我无法插入这样的范围
 start_hour = 08:00 AM和end_hour = 09:20 AM,因为09:00 AM和09:30 AM之间的范围已经存在,因此,新范围与表中存在的范围冲突。 我尝试了很多查询,但之间没有,插入的end_hour必须小于表中的start_hour。 有人知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

我假设您已将时间格式转换为hh24:mi

也许这会有所帮助:

with tab as(
select 'date1' as dat,  '09:00' as  start_hour, '09:30' as end_hour from dual union all
select 'date1' as dat,  '10:30' as  start_hour, '11:30' as end_hour from dual union all
select 'date1' as dat,  '13:00' as  start_hour, '15:00' as end_hour from dual 
)
SELECT COUNT(*)
  FROM   tab
  WHERE  start_hour <= '09:10' --:new_end_hour
  AND    end_hour   >= '07:00' --:new_start_hour
  AND    dat = 'date1'
  ;

或者您可以使用between来检查值之间的start_hourend_hour

with tab as(
select 'date1' as dat,  '09:00' as  start_hour, '09:30' as end_hour from dual union all
select 'date1' as dat,  '10:30' as  start_hour, '11:30' as end_hour from dual union all
select 'date1' as dat,  '13:00' as  start_hour, '15:00' as end_hour from dual 
)
SELECT COUNT(*)
  FROM   tab
  WHERE  ('09:00' between start_hour and end_hour
  or    '09:10' between start_hour and end_hour
  )
  AND    dat = 'date1'
  ;

db <>提琴here

答案 1 :(得分:0)

我已经找到了解决我问题的方法,作为对我的问题的评论的推荐,当我更改小时数格式和新小时数格式时,它可以很好地工作。 这是将来遇到同样问题的人的代码。

DECLARE
  l_count NUMBER(1) := 0;
BEGIN

  SELECT COUNT(*)
  INTO   l_count
  FROM   table
  WHERE  start_hour <= :new_start_hour
  AND    end_hour   >= :new_end_hour
  AND    date = :date
  AND    ROWNUM     = 1;
  dbms_output.put_line(l_count);

END;
/

感谢我们的所有帮助。