Sql 如何处理横截面日期
SRC_记录(来自SRC的记录) TGT_记录中提供的记录Sql 如何处理横截面日期,sql,sql-server,Sql,Sql Server,SRC_记录(来自SRC的记录) TGT_记录中提供的记录 Str_id Eff_From_Dt Eff_to_dt str_code 12 08-Jul-13 31-Aug-13J 161 12 01-Jan-14 04-Jan-14J 161 12 15-Jan-14 30-Jan-14J 161 我用一个存储过程创建了上面的代码,我在其中使用 (@start_dt betw
Str_id Eff_From_Dt Eff_to_dt str_code
12 08-Jul-13 31-Aug-13J 161
12 01-Jan-14 04-Jan-14J 161
12 15-Jan-14 30-Jan-14J 161
我用一个存储过程创建了上面的代码,我在其中使用
(@start_dt between effective_from_dt and effective_to_dt) OR
(@end_dt between effective_from_dt and effective_to_dt)
但我没有得到预期的结果。我想从src
中拒绝间隔(01-Sep-13,01-Feb-14)
,因为它与tgt
中的现有可用记录重叠
请帮帮我,我有麻烦了。听起来您正在测试一个时间段,以查看它是否与表中的任何现有时间段重叠 让我们简化一下,看一个检查调用,一个时间段s1-e1,第二个时间段s2-e2 目前,在您的
BETWEEN
语句中,您正在检查s2是否在s1和e1之间,或者e2是否在s1和e1之间。你成功抓住了前三个案子
案例1
s1 e1
|--------|
s2 e2
|---------|
案例2
s1 e1
|--------|
s2 e2
|---------|
案例3
s1 e1
|--------------------|
s2 e2
|------------|
然而,有一个案例你没有抓住。考虑现有时间段S1-E1可能完全落在您测试的时间段内的可能性。s2和e2都不在s1和e1之间,但时间段肯定重叠
案例4
s1 e1
|--------|
s2 e2
|------------------|
您需要调整WHERE
子句条件,以检查s2
和e2>e1
在您的示例中,应该拒绝范围
01-Sep-13->01-Feb-14
,因为它包含01-Jan-14->04-Jan-14
和15-Jan-14->30-Jan-14
。如果您修改查询以检查上述情况4,则此重叠也将根据需要被拒绝。似乎您正在使用SQLServer2000,下面是一个解决方案。它只选择“好”行
declare @TGT_RECORD table (Str_id INT, Effective_From_Dt DATE, Effective_to_dt DATE, str_code INT)
insert @TGT_RECORD values
(12, '08-Jul-13','31-Aug-13', 161),
(12, '01-Jan-14','04-Jan-14', 161),
(12, '15-Jan-14','30-Jan-14', 161)
declare @SRC_RECORD table (Str_id INT, Effective_From_Dt DATE, Effective_to_dt DATE, str_code INT)
insert @SRC_RECORD values
(12, '01-Sep-13','01-Feb-14',161),
(12, '01-Sep-13','31-Dec-13',161),
(12, '05-Jan-14','14-Jan-14',161)
select t1.Str_id, t1.Effective_From_Dt Eff_from_Dt, t1.Effective_to_dt Eff_to_Dt, t1.str_code--, case when a.chk is not null then 'Reject' else 'Insert' end Action
from @SRC_RECORD t1
where not exists
(
select 1 from
@TGT_RECORD t2
where
t1.Effective_From_Dt between t2.Effective_From_Dt and t2.Effective_to_dt
or t1.Effective_to_Dt between t2.Effective_From_Dt and t2.Effective_to_dt
or t2.Effective_From_Dt between t1.Effective_From_Dt and t1.Effective_to_dt
)
请您将问题的格式改为有意义的?您能给我们看一下整个过程/插入语句,而不仅仅是
WHERE
子句(部分)吗?我们也不知道@start\u dt
和@end\u dt
的含义以及它们与其余部分的关系。是的,我需要处理上述所有场景,但我无法处理。一种方法是将测试子句更改为(@start\u dt介于effective\u from\u dt和effective\u to\u dt之间)或(@end_dt介于effective_from_dt和effective_to_dt之间)或(@start_dteffective_to_dt))
。我认为@t-clausen.dk的check子句也应该起作用……人们似乎总是把重叠测试复杂化了。如果周期1开始于周期2结束之前,那么两个周期重叠。(即,从上述示例中,您需要的唯一测试是s1是的,它正在工作,我做了:-)谢谢alot@Rahul-S-Vig太棒了!您可以通过单击复选标记来接受答案。
declare @TGT_RECORD table (Str_id INT, Effective_From_Dt DATE, Effective_to_dt DATE, str_code INT)
insert @TGT_RECORD values
(12, '08-Jul-13','31-Aug-13', 161),
(12, '01-Jan-14','04-Jan-14', 161),
(12, '15-Jan-14','30-Jan-14', 161)
declare @SRC_RECORD table (Str_id INT, Effective_From_Dt DATE, Effective_to_dt DATE, str_code INT)
insert @SRC_RECORD values
(12, '01-Sep-13','01-Feb-14',161),
(12, '01-Sep-13','31-Dec-13',161),
(12, '05-Jan-14','14-Jan-14',161)
select t1.Str_id, t1.Effective_From_Dt Eff_from_Dt, t1.Effective_to_dt Eff_to_Dt, t1.str_code--, case when a.chk is not null then 'Reject' else 'Insert' end Action
from @SRC_RECORD t1
where not exists
(
select 1 from
@TGT_RECORD t2
where
t1.Effective_From_Dt between t2.Effective_From_Dt and t2.Effective_to_dt
or t1.Effective_to_Dt between t2.Effective_From_Dt and t2.Effective_to_dt
or t2.Effective_From_Dt between t1.Effective_From_Dt and t1.Effective_to_dt
)