Thank you for the Reply!.. my criteria is that we would have mutiple records (Start and EndDt's) in the @Table1 for single ID and also the same in the @table2. Your query pickup the records the correct records from @Table1 who's date are out of range. below is the data that i have. i would need the out from the @Table1 (ID,StartDt,EndDt) which dont fall in the range of the @table2.
Thank you once again for taking time to help me out.
declare @Table1 table
(id int,
StartDt datetime,
EndDt datetime)
insert into @Table1
select 1,'2009-11-28 00:00:00.000',''
union all
select 1,'2009-11-07 00:00:00.000','2009-11-27 00:00:00.000'
union all
select 1,'2009-07-01 00:00:00.000','2009-11-06 00:00:00.000'
union all
select 1,'2009-06-19 00:00:00.000','2009-06-30 00:00:00.000'
union all
select 2,'2009-11-28 00:00:00.000',''
union all
select 2,'2009-11-07 00:00:00.000','2009-11-27 00:00:00.000'
union all
select 2,'2009-07-01 00:00:00.000','2009-11-06 00:00:00.000'
union all
select 2,'2009-06-19 00:00:00.000','2009-06-30 00:00:00.000'
declare @Table2 table
(id int,
StartDt datetime,
EndDt datetime)
insert into @Table2
select 1,'2008-11-05 00:00:00.000','2009-11-06 00:00:00.000'
union all
select 1,'2007-11-29 00:00:00.000','2008-11-04 00:00:00.000'
union all
select 1,'2007-09-04 00:00:00.000','2007-09-04 00:00:00.000'
union all
select 1,'2007-07-02 00:00:00.000','2007-07-12 00:00:00.000'
union all
select 1,'2006-09-05 00:00:00.000','2007-06-21 00:00:00.000'
union all
select 2,'2008-11-05 00:00:00.000','2009-11-06 00:00:00.000'
union all
select 2,'2007-11-29 00:00:00.000','2008-11-04 00:00:00.000'
union all
select 2,'2007-09-04 00:00:00.000','2007-09-04 00:00:00.000'
union all
select 2,'2007-07-02 00:00:00.000','2007-07-12 00:00:00.000'
union all
select 2,'2006-09-05 00:00:00.000','2007-06-21 00:00:00.000'
;WITH pivotedData AS
(SELECT id, StartDt as TestDT
FROM@Table1
UNION ALL
SELECT id, EndDt AS TestDT
FROM@Table1
)
SELECT DISTINCT
pd.ID
FROM
pivotedData AS pd
LEFT JOIN
@table2 AS t2
ONpd.TestDT >= t2.StartDt
AND pd.TestDt <= t2.EndDt
WHERE
t2.id IS NULL