If you want to get 50 rows before and after, perhaps this will do what you want:
with cte1 as (
select top 50 t.*
from table t
where CalculatedDate <= getdate()
order by CalculatedDate desc
),
cte2 as (
select top 50 t.*
from table t
where CalculatedDate > getdate()
order by CalculatedDate
)
select *
from (select * from cte1 union all select * from cte2) t
EDIT:
It is not clear to me from the context of the question whether a row number is actually needed. It is easy enough to add, thoug:
(select top 50 t.*,
- row_number() over (order by CalculatedDate desc) as rownumber
from table t
where CalculatedDate <= getdate()
order by CalculatedDate desc
)
union all
(select top 50 t.*,
row_number() over (order by CalculatedDate) as rownumber
from table t
where CalculatedDate > getdate()
order by CalculatedDate
)
You can actually combine these into one query:
select t.*,
((case when CalculatedDate < getdate() then -1 else 1 end) *
(row_number() over (partition by (case when CalculatedDate < getdate() then 1 else 0 end)
order by (case when CalculatedDate < getdate() then CalculatedDate end) desc,
CalculatedDate asc
)
)) as rn
from table t;
You can put this in a subquery and select where rn
between -50 and 50.
However, I'm not sure what to do about row number 0 and the question provides no information on what to do with any records that match getdate()
(as unlikely as that is). I think the first answer does what that OP needs.