Numbering Rows Within Groups
A window row number ranks each row's position in its ordered partition. The ordering should determine ties when exact positions are important.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace two categories
Four source rows split into two categories can receive row numbers one and two within each category. Ordering each partition by date and ID gives a repeatable sequence. Two-field ordering supplies an explicit tie rule.
Select a first per partition deliberately
If displaying the latest item for each category, order newest first under the intended timestamp and choose number one. Stable query limits illustrate a similar single-list selection.[1]
Break ties while producing per-group positions
To rank work in two queues, partition by queue ID and order by descending priority and ascending stable task ID. The highest priority in each queue gets row number one. Equal priority within a queue uses its task ID. See two-column ordering.
Queue A: ID 7 priority 9 → position 1
Queue A: ID 8 priority 9 → position 2
Queue B: ID 9 priority 8 → position 1
Use an outer filter to select based on computed rank
A query can compute row numbers in a subquery and select position one outside it. Decide whether ties get distinct positions or a shared rank. Test two equal keys in the same partition.