2

I found this solution on the SQL Server forum on how to reorder records in a table.

UPDATE SomeTable
SET rankcol = SubQuery.Sort_Order
FROM
    (
    SELECT IDCol, Row_Number() OVER (ORDER BY ValueCOL) as SORT_ORDER
    FROM SomeTable
    ) SubQuery
INNER JOIN SomeTable ON
SubQuery.IDCol = SomeTable.IDCol

When I try doing the same on PostgreSQL, I get an error message -

ERROR: table name "sometable" specified more than once

Any help will be appreciated.

Thanks!

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
user558122
  • 851
  • 3
  • 11
  • 15

1 Answers1

5

You don`t need to explicitly join SomeTable, how cool is that? :)

UPDATE SomeTable
SET rankcol = SubQuery.Sort_Order
FROM
    (
    SELECT IDCol, Row_Number() OVER (ORDER BY ValueCOL) as SORT_ORDER
    FROM SomeTable
    ) SubQuery
where SubQuery.IDCol = SomeTable.IDCol

remark: Postgres is case insensitive, better use lower-case, like row_number, sort_order, id_col , etc.

maniek
  • 7,087
  • 2
  • 20
  • 43