set @rn = 0;
with tasks as (
select distinct Start_Date, End_Date
from Projects
order by Start_Date
)
select
Start_Date, End_Date,
case
when lag(End_Date, 1) over(Order by Start_Date) != Start_Date then @rn:=@rn+1
else @rn
end as cnt
from tasks
In the above query @rn := @rn+1
is executed even when the when
condition is false
.
I encountered this issue which trying to solve a problem from HackeRank.
If someone can explain why @run := @rn+1
is executed even when the condition is false
it would be great.
Here is the output that the above query is generating for the specified problem :
2015-10-01 2015-10-02 0
2015-10-02 2015-10-03 1
2015-10-03 2015-10-04 2
2015-10-04 2015-10-05 3
2015-10-11 2015-10-12 24
2015-10-12 2015-10-13 5
2015-10-15 2015-10-16 24
2015-10-17 2015-10-18 24
2015-10-19 2015-10-20 24
2015-10-21 2015-10-22 24
2015-10-25 2015-10-26 24
...
New contributor