如何判断时间的交叉(300分)

L

lingxin

Unregistered / Unconfirmed
GUEST, unregistred user!
Table
ID Type SDate EDate
1 A 2002-05-10 2002-05-26
2 A 2002-05-26 2002-05-30
3 B 2002-05-15 2002-05-18
4 A 2002-05-15 2002-05-30

后台SQL Server 2000
此时通过SQL语句判断出A的时间是交叉的
但当ID:4删除后判断出A的时间不是交叉的

这个SQL语句如何写了


 
必须写sql语句么??为什么不能用delphi编程判断?
 
select type,datediff(day,min(sdate),max(edate)),sum(datediff(day,sdate,edate))
from tablename
group by type
对不起,没试过。
我的 想法是,取出起始时间的最小值和结束时间的最大值,然后求出他们之间的天数
然后求出其他的 天数之和,大于肯定有重复,但这考虑不全面
 
select a.SDate-b.EDate from
( select ID,SDate from Table where Type = 'A') a,
( select ID,EDate from Table where Type = 'A') b
where a.ID<>b.ID
and a.ID>b.ID
如果查询结果中有负数说明有交叉。
不知道是否符合你的要求。
 
如果不存在交叉,则按SDate和EDate产生的排序应当完全一致,否则就存在交叉.
select num=identity(int,1,1),* into t1 from tablename order by SDate
select num=identity(int,1,1),* into t2 from tablename order by EData
delete t2 from t2,t1 where t2.num=t1.num and t2.ID=t1.ID
if exist(select * from t2)
begin
--存在交叉
end
使用此钟方法也可以删除交叉行
 
你可以用一句Sql表达。开始时间用S,结束时间用E.
对于一个记录i,则 如果Sx<Si 必须Ex<Si, 如果Ex>Ei 必须Sx>Ei.
但这个表嵌套是非常耗时的。最佳的算法是用存储过程实现或代码。计算复杂度 N * N
首先得到中间表:
select * from x order by Type,SDate
然后一个循环就搞定了。条件是 与上一条记录 Type 相同,如果有一个 Si - E(i-1)<0
这个type 有相交。 计算复杂度 N
速度差别是天壤之别。
 
思路:
select min(SDate) from table1 --> '2002-5-10'
select max(EDate) from table1 --> '2002-5-30'
相差20天
for i=0 to 20 do
begin
select * from table1 where SDate <= '2002-05-10'+i天 and EDate >='2002-05-10'+i天 group by type
// 如果每种type有两条或两条以上记录,则时间重叠
end
 
要对每条记录遍历
对于每一条记录,
开始时间@Sx,结束时间@Ex,@id,@type
如果
select * from table where ((@Sx between Sdate and Edate) or
@Ex (between Sdate and Edate)) and id<>@id and type=@type
有记录说明这条记录存在交叉,如果没有记录就没有交叉

 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
896
DelphiTeacher的专栏
D
S
回复
0
查看
987
SUNSTONE的Delphi笔记
S
S
回复
0
查看
805
SUNSTONE的Delphi笔记
S
顶部