1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| With MaTable (dt, val) as
(
select {d '2010-01-01'}, 0 union all
select {d '2010-10-10'}, 1 union all
select {d '2010-10-20'}, 1 union all
select {d '2010-10-25'}, 1 union all
select {d '2010-10-30'}, 0 union all
select {d '2010-10-31'}, 1
)
, SR (dt, [first]) as
(
select min(dt), 1
from MaTable
where val = 1
)
select mt.dt, mt.val, coalesce(SR.[first], 0) as [first]
from MaTable as mt
left outer join SR
on SR.dt = mt.dt
dt val first
---------- ----------- -----------
01/01/2010 0 0
10/10/2010 1 1
20/10/2010 1 0
25/10/2010 1 0
30/10/2010 0 0
31/10/2010 1 0 |
Partager