Postgres: Find position of a specific row within a resultset?

Viewed 9773

I'm trying to figure out how to get the relative position of a single item in a query relative to all the items returned from the query.

For example,the long hand way of getting the answer would be:

single_item = SELECT * FROM table WHERE id=65
result = SELECT * FROM table WHERE published_date < date_value
x=1
foreach(result as item):
    if(item.id == single_item.id):
        required_value = x
    endif
    x++
endforeach

Is there a simple way of getting required_value just through a single postgres query?

1 Answers
Related