日期:2014-05-17 浏览次数:20565 次
use tempdb
go
create table #a( dt1 datetime,dt2 datetime)
insert into #a select '2013-01-01','2013-01-31'
insert into #a select '2013-02-10','2013-02-20'
insert into #a select '2013-03-05','2013-03-31'
insert into #a select '2013-04-01','2013-04-30'
create table #b( dt1 datetime,dt2 datetime)
insert into #b select '2013-01-10','2013-01-15'
insert into #b select '2013-02-05','2013-02-10'
insert into #b select '2013-03-06','2013-03-20'
go
select
b.*,Flag=case when a.dt1 is not null then 1 else 0 end
from #b as b
left join #a as a on a.dt1<=b.dt1 and a.dt2>=b.dt2
/*
dt1 dt2 Flag
2013-01-10 00:00:00.000 2013-01-15 00:00:00.000 1
2013-02-05 00:00:00.000 2013-02-10 00:00:00.000 0
2013-03-06 00:00:00.000 2013-03-20 00:00:00.000 1
*/