问题
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:
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;
来源:https://stackoverflow.com/questions/40323982/row-number-in-query-result