sql >> Base de Datos >  >> RDS >> Sqlserver

Una actualización con múltiples condiciones. SQL 2008

Puede averiguar qué usuario tiene la prioridad más alta usando row_number() . SQL Server le permite hacer esto en un CTE actualizable, por lo que la consulta se ve así:

with toupdate as (
      select t.*,
             row_number() over (partition by projectid
                                order by (case when userid = 1 then 1
                                               when userid = 2 then 2
                                               when userid = 3 then 3
                                               else 4
                                          end
                                         )
                               ) as PriorityForLead
      from table t
     )
update toupdate
    set RoleId = 11
    where PriorityForLead = 1;