[Firebird] How to select, find result/row number and update a field using 1 query?

I'm sorting a table by points (sports sim) and I would like to figure out how I can get the row/result number saved to a field called Position?

So, basically the first result would have its Position set to '1', the second result would have its position set to '2' etc..
Lag.close;
Lag.sql.text := 'select Klubb, GP, W, L, D, PTS, GF, GA from Lag where division = ''NHL'' order by PTS DESC, W DESC, GF DESC, Klubb ASC';
Lag.Open;

Open in new window

MerlaP83Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nick UpsonPrincipal Operations EngineerCommented:
the nearest you can do on the firebird SQL side is to have a call to gen_id in the select so that they are numbered in order (but not starting at one except the very first time)
0
Nguyen Huu PhuocSenior ManagerCommented:
You are using Delphi!?
Why don't you use event in the component to set value for Position filed.
I don't know what db control you use.
0
FVERCommented:
Hi,

I may arrive late after the battle has ended, but this is a solution I use sometimes, when performance is not essential.
Please note that this is a rank fonction, that is to say that if several lines have the same number of points, they will have the same position... To avoid this, you may add more conditions in the where clause of the subquery.
--if position might not be unique :
select Lag1.Klubb, Lag1.GP, Lag1.W, Lag1.L, Lag1.D, Lag1.PTS, Lag1.GF, Lag1.GA,
       (select count(*)+1 from Lag Lag2 where Lag2.PTS < Lag1.PTS) as Position
  from Lag Lag1
 where Lag1.division = 'NHL'
 order by Lag1.PTS DESC, Lag1.W DESC, Lag1.GF DESC, Lag1.Klubb ASC
 
--if position has to be unique :
select Lag1.Klubb, Lag1.GP, Lag1.W, Lag1.L, Lag1.D, Lag1.PTS, Lag1.GF, Lag1.GA,
       (select count(*)+1 from Lag Lag2 where Lag2.PTS < Lag1.PTS
                                           OR (Lag2.PTS = Lag1.PTS AND Lag2."another column" < Lag1."another column")
       ) as Position
  from Lag Lag1
 where Lag1.division = 'NHL'
 order by Lag1.PTS DESC, Lag1.W DESC, Lag1.GF DESC, Lag1.Klubb ASC

Open in new window

0
twinsoftCommented:
Hi, there are two ways to do this. One is to use rdb$get_context/rdb$set_context functions to create a dynamic variable to store the successive number of each row.

select rdb$get_context('USER_TRANSACTION', 'COUNTER') as recposition, rdb$set_context('USER_TRANSACTION', 'COUNTER', coalesce(cast(rdb$get_context('USER_TRANSACTION', 'COUNTER') as integer), 0) + 1), Klubb, GP, W, L, D, PTS, GF, GA from Lag where division = 'NHL' order by PTS DESC, W DESC, GF DESC, Klubb ASC

the second way is to make a selectable stored procedure and use a select like this
select Klubb, GP, W, L, D, PTS, GF, GA from SP_Lag('NHL')
In that SP you will have an outpout variable named position and you will put the value to it by using something like position = :position + 1;

create or alter procedure SP_Lag (
    i_name varchar(100),
returns (
    position integer, Klubb, GP, W, L, D, PTS, GF, GA)
as
begin
 position = 1;
 for
   select Klubb, GP, W, L, D, PTS, GF, GA
     from Lag
  where division = :i_name
 order by PTS DESC, W DESC, GF DESC, Klubb ASC
   into :Klubb, :GP, :W, :L, :D, :PTS, :GF, :GA
 do
  begin
    position = :position + 1;
    suspend;
 end
end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.