select tmp.id, tmp.portfolio,
ROW_NUMBER() OVER(order by tmp.id DESC) AS RowNum
from
(select r.portfolio, r.id
from research r
where r.created_by = 'Adam Cohen'
) as tmp
WHERE RowNum BETWEEN 5 AND 10;
I am not able refer the RowNum in the where condition, as it shows that Invalid column name 'RowNum'. Kindly help me to use the right syntax to get the result.
Edit - Changed Requirement
SELECT * FROM
(
select id, portfolio,
CASE WHEN l.posted_on IS NULL
THEN CONVERT(VARCHAR(40),l.created_on,120)
ELSE CONVERT(VARCHAR(40),l.posted_on,120)
END AS sort_by,
ROW_NUMBER() OVER(order by sort_by DESC) AS RowNum
from research
where created_by = 'Adam Cohen'
) x
WHERE x.RowNum BETWEEN 5 AND 10
I tried to include the Row_Number
function like above but I got the sort_by
as invalid column.