-3
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.

2 Answers 2

4

You'll need to wrap your projection in a derived table, although you can drop the inner tmp table:

SELECT * FROM
(
    select id, portfolio,
       ROW_NUMBER() OVER(order by id DESC) AS RowNum 
    from 
       research
    where created_by = 'Adam Cohen'
) x
WHERE x.RowNum BETWEEN 5 AND 10

Edit

Note that if you don't actually need the pseudo row number in the final select, that since Sql 2012, in your 'page of data' scenario, that you will be able to use OFFSET FETCH apply the without the need for ROW_NUMBER() or derived tables at all:

select id, portfolio
from research
where created_by = 'Adam Cohen'
order by id desc
offset 5 rows fetch next 6 rows only;

Edit #2, Re new requirements

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

Can be more concisely expressed as

CONVERT(VARCHAR(40), COALESCE(posted_on, created_on)) AS AS sort_by

However, if you still need original row number, you also use this projection in the ORDER BY of the windowing function (OVER), in order to DRY this up you will need the nested derived table. You can still use the SQL 2012 OFFSET / FETCH NEXT to paginate, however:

SELECT *,
       ROW_NUMBER() OVER(order by sort_by DESC) AS RowNum 
FROM
(
    SELECT id, portfolio, CONVERT(VARCHAR(40), COALESCE(posted_on, created_on)) AS sort_by
    from research 
    where created_by = 'Adam Cohen' 
) y
ORDER BY id DESC
OFFSET 5 ROWS FETCH NEXT 6 ROWS ONLY;
3
  • 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 sory_by as invalid column.
    – Vijay VSTO
    Jun 4, 2015 at 17:48
  • @VijayVSTO Well, l is presumably an alias for a new table which wasn't referenced in your original question, and since you've now introduced a CASE WHEN projection into the mix, things are obviously radically different from your original question. Please post an MVCE of the actual problem and we can help.
    – StuartLC
    Jun 4, 2015 at 17:57
  • Sorry for the inconvenience. I have two columns created_on and posted_on in the table. I have to take the created_on column if the posted_on date is null. Then I need to fetch the in between records.
    – Vijay VSTO
    Jun 4, 2015 at 18:08
2

You need to nest the ROW_NUMBER in the Derived Table:

select tmp.id, tmp.portfolio, tmp.RowNum
from 
  (select r.portfolio, r.id,
      ROW_NUMBER() OVER(order by r.id DESC) AS RowNum 
   from research r 
   where r.created_by = 'Adam Cohen'
 ) as tmp 
WHERE RowNum BETWEEN 5 AND 10;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.