Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Selecting dates not in a date set
Message
From
19/07/2007 13:04:52
John Baird
Coatesville, Pennsylvania, United States
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01241975
Message ID:
01242057
Views:
29
>
>DECLARE @dBegin datetime, @dEnd datetime
>SET @dBegin = '20060101'
>SET @dEnd   = '20070719'
>
>DECLARE @Test TABLE (Fund char(3), trans_date datetime)
>INSERT INTO @Test VALUES ('ABC','20060101')
>INSERT INTO @Test VALUES ('ABC','20060102')
>INSERT INTO @Test VALUES ('ABC','20060103')
>
>INSERT INTO @Test VALUES ('DEF','20070101')
>
>DECLARE @Range TABLE (range_date datetime)
>INSERT INTO @Range
>SELECT * FROM (
>select @dBegin + b1+b2+b3+b4+b5+b6+b7+b8+b9+b10+b11+b12 range_date
>from       (select 0 b1  union select 1    b1)  t1
>cross join (select 0 b2  union select 2    b2)  t2
>cross join (select 0 b3  union select 4    b3)  t3
>cross join (select 0 b4  union select 8    b4)  t4
>cross join (select 0 b5  union select 16   b5)  t5
>cross join (select 0 b6  union select 32   b6)  t6
>cross join (select 0 b7  union select 64   b7)  t7
>cross join (select 0 b8  union select 128  b8)  t8
>cross join (select 0 b9  union select 256  b9)  t9
>cross join (select 0 b10 union select 512  b10) t10
>cross join (select 0 b11 union select 1024 b11) t11
>cross join (select 0 b12 union select 2048 b12) t12
>where @dBegin+b1+b2+b3+b4+b5+b6+b7+b8+b9+b10+b11+b12 <= @dEnd) r
>
>select Tbl2.range_date, Tbl2.Fund
>from  @Test b
>right join (select Rng.range_date, Tbl1.Fund
>                   FROM @Range Rng
>            FULL JOIN (SELECT DISTINCT Fund FROM @Test) Tbl1
>            ON 1 = 1) Tbl2
>on b.trans_date = Tbl2.range_date AND b.Fund = Tbl2.Fund
>WHERE b.Fund IS NULL
>order by Tbl2.Fund, Tbl2.range_date
>
Thanks for the help.
Previous
Reply
Map
View

Click here to load this message in the networking platform