简体   繁体   中英

How can I rearrange and update a sequence of number in postgresql?

In my table, each row can hold an integer let's call this integer column height . for example, the values in this column could be: [5, 4, 0, 1, 9] I need for this sequence in the example to be rearranged to become: [0, 1, 4, 5, 9] and then I want these values to become like this: [1, 2, 3, 4, 5] .

The idea I am trying to write in SQL is to get the minimum at the first time and count how many values are there less than it.

How can I write/translate this idea into an SQL Query?

You seem to want a ranking:

select t.*, row_number() over (order by height) as height_seqnum
from t
order by height_seqnum;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM