Autoincrement value in a SELECT statement

Поиск
Список
Период
Сортировка
Искать

Autoincrement value in a SELECT statement

От:
JORGE MALDONADO <jorgemal1960@gmail.com>
Дата:
Hi,

Let's suppose that I have a table like this:

team_name       varchar(120)
score                integer

I have a "SELECT team_name, score FROM teams ORDER BY score DESC". This mean that the first position belongs to the highest score, second position to the next score, and so on.What I need is to have the position.

position 1 for the highesst score
position 2 for the next score
.
.
.

Is it possible to include a field in the SELECT statement which "kind of autoincrements" so I can get the position?

Respectfully,
Jorge Maldonado

Re: Autoincrement value in a SELECT statement

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
JORGE MALDONADO  writes:
> Is it possible to include a field in the SELECT statement which "kind of
> autoincrements" so I can get the position?

The row_number() window function should do that for you, or you might
want rank() depending on what you think should happen for ties.
		regards, tom lane


FAQ