PostgreSQL Record Reordering using Update with a Sub-Select

前端 未结 1 1910
迷失自我
迷失自我 2021-01-23 08:35

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 IDC         


        
相关标签:
1条回答
  • 2021-01-23 09:22

    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.

    0 讨论(0)
提交回复
热议问题