Though I agree with others that you could use count()
to get the total number of rows, here is how you can use the row_count()
:
To get the total no of rows:
with temp as (
select row_number() over (order by id) as rownum
from table_name
)
select max(rownum) from temp
To get the row numbers where name is Matt:
with temp as (
select name, row_number() over (order by id) as rownum
from table_name
)
select rownum from temp where name like 'Matt'
You can further use min(rownum)
or max(rownum)
to get the first or last row for Matt respectively.
These were very simple implementations of row_number()
. You can use it for more complex grouping. Check out my response on Advanced grouping without using a sub query