SQL Flow Field Guide

Rank rows inside a group

Add a row number while keeping each source row in the output.

What you are trying to do

A report needs the top synthetic item in each team with stable tie handling.

Work through it

  1. Read the partition key and order keys.
  2. Trace how the item ID breaks equal scores.
  3. Compare the row number with the original score.

Check your work

Each team starts at row number one and every input row remains visible.

Practice SQL

Open in SQL Flow
SELECT Team, ItemId, Score, ROW_NUMBER() OVER (PARTITION BY Team ORDER BY Score DESC, ItemId) AS Position FROM (VALUES (N'North', 11, 90), (N'North', 12, 90), (N'South', 21, 84)) AS Result(Team, ItemId, Score) ORDER BY Team, Position;

Commands in this lesson

Next lesson Change a temporary table