表1 工作安排记录  主要字段: 预约开始时间、预约结束时间
表2 工作表 主要字段: 开始时间、结束时间  允许延时要求判断能否安排表二的工作,工作必须连续。例:表一记录: 6时 8时
               10时 14时 
               18时 22时
表二 7时 10时  允许延时7小时可以看出工作可以安排在14时到17时sql语句应该怎么写?

解决方案 »

  1.   

    有点小复杂啊
    select d.endtime1,d.endtime1+delay from 
              (select starttime+delay,starttime,delay,endtime-starttime as m from t2) c,          (select a.endtime1,a.starttime1-b.endtime1 as m from
                     (select rownum,starttime1,endtime1 from t1 order by starttime1) a,
                   (select rownum,starttime1,endtime1 from t1 order by starttime1) b 
                    where a.rownum=(b.rownum+1)
             ) d where c.starttime=d.endtime1 and d.m>=a.endtime1
    没测试,应该可以。
    思路:查询表一记录之间的间隔时间(可安排工作的空闲)
    然后去查找表二的工作需要时间
    根据这两个值去关联表进行查询,(正好因为你的时间要连续,这样关联很方便查询。)
      

  2.   

    更改一下,上面遗漏了一个条件,rownum也没有转换。思路还是同上。已测试CREATE TABLE ssys.t1
     (
      s                          NUMBER(2),
      e                          NUMBER(2)
     )
     PCTFREE    10
     PCTUSED
     INITRANS   1
     MAXTRANS   255
     TABLESPACE dbmistabspace
     STORAGE   (
          INITIAL     1048576
          NEXT        1048576
          PCTINCREASE 0
          MINEXTENTS  1
          MAXEXTENTS  2147483645
          FREELIST GROUPS  0
          FREELISTS  0
       )
    /CREATE TABLE ssys.t2
     (
      s                          NUMBER(2),
      e                          NUMBER(2),
      d                          NUMBER(2)
     )
     PCTFREE    10
     PCTUSED
     INITRANS   1
     MAXTRANS   255
     TABLESPACE dbmistabspace
     STORAGE   (
          INITIAL     1048576
          NEXT        1048576
          PCTINCREASE 0
          MINEXTENTS  1
          MAXEXTENTS  2147483645
          FREELIST GROUPS  0
          FREELISTS  0
       )
    /INSERT INTO SSYS.T1
    (S,E)
    VALUES 
    (6,8)
    /
    INSERT INTO SSYS.T1
    (S,E)
    VALUES 
    (10,14)
    /
    INSERT INTO SSYS.T1
    (S,E)
    VALUES 
    (18,22)
    /
    INSERT INTO SSYS.T2
    (S,E,D)
    VALUES 
    (7,10,7)
    /最后select d.e,d.e+c.d from 
    (select s+d smax,s,d,e-s as m from t2)
    c ,
    (select b.e,a.s-b.e as m from 
        (select rownum rn,s,e from t1 order by s) a, 
        (select rownum rn,s,e from t1 order by s) b 
         where a.rn=(b.rn+1))
         d
    where c.s<=d.e and c.smax>=d.e and d.m>=c.m 结果
    14 21
      

  3.   

    上面查询的结果字段有个疏忽,应该是起始时间+差值select d.e,d.e+c.m from 
    (select s+d smax,s,d,e-s as m from t2)
    c ,
    (select b.e,a.s-b.e as m from 
        (select rownum rn,s,e from t1 order by s) a, 
        (select rownum rn,s,e from t1 order by s) b 
         where a.rn=(b.rn+1))
         d
    where c.s<=d.e and c.smax>=d.e and d.m>=c.m 结果
    14 17