With Firebird 3, you can use row_numberthe window function , for example:
select row_number() over (order by id, some_column), id, other_columns
from mytable
Or if you want to restart the count for each id value:
select row_number() over (partition by id order by some_column), id, other_columns
from mytable
If you are stuck in Firebird 2.5, you will need to apply some tricks:
execute block ( ) :
execute block
returns (counter int, id int, other_columns varchar(32))
as
begin
counter = 0;
for select id, other_columns from mytable order by id, some_column into :id, :other_columns
do
begin
counter = counter + 1;
suspend;
end
end
, , id reset id.
+ . CTE, , ( IIRC ).
, , - RDB$DB_KEY:
select rdb$db_key, id, other_columns
from mytable
order by id, some_column
RDB$DB_KEY ( , ).