Update duplicate rows only with a MAX function in SQL

前端 未结 1 1758
傲寒
傲寒 2021-01-25 23:53

I have a table like this, where, suppose for the sake of an example, NAME is a unique identifier.

NAME    AGE         VALUE
Jack    Under 65    3
Ja         


        
1条回答
  •  一向
    一向 (楼主)
    2021-01-26 00:19

    You can define custom order with case when clause and then use analytic max(). This worked for given examples:

    update t1 set age = (
        select max(age) keep (dense_rank last 
               order by case when age = 'Over 75'  then 1
                             when age = '66-74'    then 2
                             when age = 'Under 65' then 3
                             when age = '25-35'    then 4
                        end)
        from t1 tx where tx.name = t1.name )
    

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