自动生成时间间隔记录

5w9g7ksd  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(452)

我有下面的表定义和insert语句,它们工作得很好。

create table.    tour_detail (
  location_id NUMBER(4),
  time interval day to second
  )

insert into tour_detail.    
(location_id,time)
 values (3, interval '11:30' hour to minute)

下面的示例中的cte是否可以通过tour\ U detail表进行修改,以执行以下操作。
将其修改为仅保留hh24:mi集,其余字段为0。
时间是如何以val=val+rand(6,12)分钟为基础的呢。
如果val>=23:45,则可以重置为某个随机的hh24:mi。
是否可以为每个巡更hdr(巡更id)生成随机数(15-25)的巡更详细信息行。生成这些行时,仅使用location\u type='g'的locations id行
需要确保相同的位置\u id不是连续的
我目前,保持旅游详细信息(旅游时间)在mmddyyyyyyyh24:mi:ss,我只需要hh:ss。

ALTER SESSION SET.  NLS_DATE_FORMAT = 'MMDDYYYY HH24:MI:SS';

CREATE TABLE locations AS
SELECT level AS location_id,
   'Door ' || level AS location_name,

CASE.    round(dbms_random.value(1,3)) 
        WHEN 1 THEN 'A' 
        WHEN 2 THEN 'T' 
        WHEN 3 THEN 'G' 
     END AS location_type

FROM   dual
CONNECT BY level <= 25;

ALTER TABLE locations 
     ADD ( CONSTRAINT location_id_pk
   PRIMARY KEY (location_id));

CREATE TABLE tour_hdr AS
SELECT level AS tour_id,
   'Tour ' || level AS tour_name
FROM   dual
CONNECT BY level <= 15;

 ALTER TABLE tour_hdr
     ADD ( CONSTRAINT tour_hdr_id_pk
   PRIMARY KEY (tour_id));

create table tour_detail 
(
      tour_id NUMBER(4),
      tour_time DATE,
      location_id NUMBER(4)
 )

declare
  v_loc number;      
  v_prev_loc number := 0;
  v_dt date := trunc(sysdate);
  v_dt_save date := trunc(sysdate);
begin

 for trs in ( select tour_id  from tour_hdr)
  loop

   -- for each  tour generate 15 to 25 rows
    for i in 1..dbms_random.value(15, 25) loop

   -- If date >23:45:00 reset. Should I
  -- interval be used instead of date

   IF v_dt > (v_dt_save + (1/1440*1425))
   THEN 

      -- reset time 
        v_dt :=  v_dt_save;

    ELSE

    -- increase last used date by random 6 to 12 minutes

   v_dt := v_dt + dbms_random.value(6,12)/(24*60);

   END IF;

 -- get random location
  select location_id 
    into v_loc 
    from (select location_id from locations where location_type = 'G' order by dbms_random.value) 
    where rownum = 1;

   IF v_prev_loc != v_loc
   THEN
        insert into tour_detail (tour_id, tour_time, location_id)
       values (trs.tour_id, v_dt, v_loc);
       v_prev_loc := v_loc;
    END IF;

  end loop;
 end loop;
end;

我的目标是从tour\u detail中提取一个记录,并为access\u history表找到一个记录,看看警卫是否及时,早,晚,没有到达那个位置。简而言之,我需要将mmddyyyyy hh24:mi:ss与时间间隔行进行比较。这很难做到吗

q0qdq0h2

q0qdq0h21#

现在还不清楚你到底想要哪个结果,以及 location_id 应该是固定的或随机的(如果是,如何?)。
如果您希望生成具有随机间隔值的给定行数,那么一个选项是使用递归cte和 dbms_random.value . 您需要确定间隔的最大长度和迭代次数。例如,这将提供10条间隔长达12小时的记录:

with cte(rnd, n) as (
    select dbms_random.value, 1 from dual
    union all
    select dbms_random.value, n+ 1 from cte where n < 10
)
select 3 as location_id, rnd * 12 * interval '1' hour as time
from cte order by n

你可以把它变成 insert 语句,只需在查询的最开始处添加以下行:

insert into tour_detail (location_id, time)
with ...
select ...

db小提琴演示:

LOCATION_ID | TIME               
----------: | :------------------
          3 | +00 07:13:32.262069
          3 | +00 03:45:18.965145
          3 | +00 07:13:41.394986
          3 | +00 02:24:19.516714
          3 | +00 11:38:16.000109
          3 | +00 11:11:47.947329
          3 | +00 08:14:54.641965
          3 | +00 02:52:43.090595
          3 | +00 03:44:26.418538
          3 | +00 09:29:35.390110

相关问题