帮忙写个sql

todayxyz 2009-06-05 10:20:02
表1 工作安排记录 主要字段: 预约开始时间、预约结束时间
表2 工作表 主要字段: 开始时间、结束时间 允许延时

要求判断能否安排表二的工作,工作必须连续。

例:表一记录: 6时 8时
10时 14时
18时 22时
表二 7时 10时 允许延时7小时

可以看出工作可以安排在14时到17时

sql语句应该怎么写?
...全文
28 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
todayxyz 2009-06-05
  • 打赏
  • 举报
回复
谢谢
william3033 2009-06-05
  • 打赏
  • 举报
回复
上面查询的结果字段有个疏忽,应该是起始时间+差值

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

william3033 2009-06-05
  • 打赏
  • 举报
回复
更改一下,上面遗漏了一个条件,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


todayxyz 2009-06-05
  • 打赏
  • 举报
回复
主要是要求在规定的延时时间内完成,
william3033 2009-06-05
  • 打赏
  • 举报
回复
有点小复杂啊
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
没测试,应该可以。
思路:查询表一记录之间的间隔时间(可安排工作的空闲)
然后去查找表二的工作需要时间
根据这两个值去关联表进行查询,(正好因为你的时间要连续,这样关联很方便查询。)
welyngj 2009-06-05
  • 打赏
  • 举报
回复
没有看懂

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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