Skip to content
Advertisement

SQL: Sort rows based on attribute order

I want to sort an the value of an attribute (called position) to the row number of another attribute’s ordering.

Example: If I have the following attributes: position, points I’d like to order the table by points (descending), then assign position the ordered row numbers ex.

Position --- Points

1 ------------ 99

2 ------------ 97

3 ------------ 81

etc..

Advertisement

Answer

You can do that using window functions row_number()

Since the OP wants to update the records

with data
  as (
    select row_number() over(order by points desc) as rnk_position
           ,position
           ,points
      from leagueParticipantView /* Is this a view or a table*/
     where leagueName LIKE &leagueName
     )
update data
   set position = rnk_position
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement