How to use ROW_NUMBER in sqlite

Abox LsrKdz picture Abox LsrKdz · May 31, 2013 · Viewed 80k times · Source

Here is my query given below.

select * from data where value = "yes";

My id is auto increment and below there is result of given query.

id || value 
1  ||   yes
3  ||   yes
4  ||   yes
6  ||   yes
9  ||   yes

How to use ROW_NUMBER in sqlite? So that i can get result which is given below.

NoId || value 
1    ||   yes
2    ||   yes
3    ||   yes
4    ||   yes
5    ||   yes

ROW_NUMBER AS NoId.

Answer

Meherzad picture Meherzad · May 31, 2013

Try this query

select id, value, (select count(*) from tbl b  where a.id >= b.id) as cnt
from tbl a

FIDDLE

| id | value | cnt |
--------------------
|  1 |   yes |   1 |
|  3 |   yes |   2 |
|  4 |   yes |   3 |
|  6 |   yes |   4 |
|  9 |   yes |   5 |