Row number in query result

2019-09-06 10:15发布

I have query to get firms by theirs sales last year.

select
   Name,
   Sale
from Sales
order by
   Sale DESC

and I get

Firm 2 | 200 000
Firm 1 | 190 000
Firm 3 | 100 000

And I would like to get index of row in result. For Firm 2 I would like to get 0 (or 1), for Firm 3 1 (or 2) and etc. Is this possible? Or at least create some sort of autoincrement column. I can use even stored procedure if it is needed.

1条回答
男人必须洒脱
2楼-- · 2019-09-06 10:58

Firebird 3.0 supports row_number() which is the better way to do this.

However for Firebird 2.5, you can get what you want with a correlated subquery:

select s.Name, s.Sale,
       (select count(*) from Sales s2 where s2.sale >= s.sale) as seqnum
from Sales s
order by s.Sale DESC;
查看更多
登录 后发表回答