我有表A:
UID_Panel StartDate EndDate Available
3173 2014-07-14 00:00:00.000 2014-07-20 23:59:59.993 NULL
3173 2014-07-21 00:00:00.000 2014-07-27 23:59:59.993 NULL
3173 2014-07-28 00:00:00.000 2014-08-03 23:59:59.993 NULL
和表B:
uid_panel dte_down_from dte_Down_To
3173 2014-07-21 00:00:00.000 2014-07-31 00:00:00.000
我需要进行加入,并使用Available
标记后两个记录“No
列。表A中将始终存在mon-sun,而日期可以是任意随机日期范围。如果startDate
和EndDate
与下限重叠,则需要将其标记为No
。
答案 0 :(得分:0)
目前还不清楚您是想要update
还是select
。这是select
版本:
select a.*,
(case when exists (select 1
from tableb b
where b.dte_down_from <= a.EndDate and
b.dte_down_to >= a.StartDate
)
then 'No'
end) as Available
from tableA a;
请注意,这会使用exists
语句中的case
子句而不是join
。两个停机时段可能会与一周重叠,此配方可确保行不会被join
意外地乘以。
此外,它使用标准SQL。您没有指定数据库,但这应该适用于它。