tags:

views:

34

answers:

3

i get an error with this query

select ID, ROW_NUMBER() OVER(ORDER BY ID) as num from T_TASK where ROW_NUMBER() = 5

and this one

select ID, ROW_NUMBER() OVER(ORDER BY ID) as num from T_TASK where num = 4

whats wrong with the queries?

+5  A: 

Use a subquery:

SELECT ID
FROM (
    SELECT ID, ROW_NUMBER() OVER(ORDER BY ID) AS num
    FROM T_TASK
) T1 WHERE num = 5
Mark Byers
+5  A: 
SELECT ID
FROM
(select ID, ROW_NUMBER() OVER(ORDER BY ID) as rownum from T_TASK) dr
WHERE rownum = 5
Mitch Wheat
A: 

1 You can't use windows functions directly on the WHERE clause

2 The same is applied to alias name too

Madhivanan