in der Version 9.x gibt es row_number().
ich habe folgendes "Problem" : bin in Col1 mit ORDER BY Col1 und stehe auf BBB / LLL
nun klicke ich auf den Header von Col2 und er ändert die Sortierung auf ORDER BY Col2
leider stehe ich aber damit nicht mehr auf meinem "Record" BBB / LLL, also muss ich ihn re-positionieren.
Code: Alles auswählen
ORDER BY Col1
--------------
Col1 Col2
AAA CCC
BBB LLL -> hier stehe ich -> __record = 2
CCC ZZZ
DDD KKK
EEE DDD
ORDER BY Col2
--------------
AAA CCC -> jetzt steh ich hier
EEE DDD
DDD KKK
BBB LLL -> und hier will ich hin -> __record = 2
CCC ZZZ
nun bekam ich den Tip mit "row_number()"
Code: Alles auswählen
test=# create table ohr(col1 text, col2 text);
CREATE TABLE
test=*# copy ohr from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> aaa ccc
>> bbb lll
>> ccc zzz
>> ddd kkk
>> eee ddd
>> \.
test=*# commit;
COMMIT
test=# select col1, col2, row_number() over (order by col1) from ohr order by col1;
col1 | col2 | row_number
------+------+------------
aaa | ccc | 1
bbb | lll | 2
ccc | zzz | 3
ddd | kkk | 4
eee | ddd | 5
(5 rows)
test=*# select col1, col2, row_number() over (order by col1) from ohr order by col2;
col1 | col2 | row_number
------+------+------------
aaa | ccc | 1
eee | ddd | 5
ddd | kkk | 4
bbb | lll | 2
ccc | zzz | 3
(5 rows)
test=*# select col1, col2, row_number() over (order by col2) from ohr order by col1;
col1 | col2 | row_number
------+------+------------
aaa | ccc | 1
bbb | lll | 4
ccc | zzz | 5
ddd | kkk | 3
eee | ddd | 2
(5 rows)
test=*# select col1, col2, row_number() over (order by col2) from ohr order by col2;
col1 | col2 | row_number
------+------+------------
aaa | ccc | 1
eee | ddd | 2
ddd | kkk | 3
bbb | lll | 4
ccc | zzz | 5
(5 rows)