Start a conversation ↗

FORMULA & SQL / TOTALS

Rank rows within each group.
Tested, not guessed.

The Excel and Google Sheets formula and the SQL query, each run on the rows below. Open this with your own data ↗

The example rows

RowA: RegionB: RepC: AmountD: Order DateE: StatusF: Customer
2NorthAna1,2002026-03-01PaidJo Smith
3SouthBen4502026-03-15OpenRaj Patel
4NorthCy3002026-04-02PaidLee Wong
5EastAna980.502026-04-20PaidMia Ross
6NorthBen752026-05-05OpenJo Smith
7WestDee1,6402026-05-18PaidSam Okoro

The formula

Excel
=COUNTIFS(A:A,A2,C:C,">"&C2)+1

The number of rows where the Region column (A:A) is Region (A2) and the Amount column (C:C) is more than Amount (C2) plus 1.

Run on the example rows:

RowResult
21
31
42
51
63
71

The SQL

PostgreSQL
SELECT *, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank_in_group
FROM sales

Run on the example rows:

regionrepamountorder_datestatuscustomerrank_in_group
EastAna980.52026-04-20PaidMia Ross1
NorthAna12002026-03-01PaidJo Smith1
NorthCy3002026-04-02PaidLee Wong2
NorthBen752026-05-05OpenJo Smith3
SouthBen4502026-03-15OpenRaj Patel1
WestDee16402026-05-18PaidSam Okoro1
Other databases
MySQL
SELECT *, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank_in_group
FROM sales
SQL Server
SELECT *, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank_in_group
FROM sales
SQLite
SELECT *, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank_in_group
FROM sales
BigQuery
SELECT *, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank_in_group
FROM sales
Snowflake
SELECT *, RANK() OVER (PARTITION BY region ORDER BY amount DESC) AS rank_in_group
FROM sales

Your own data

Paste your rows into the free tool and it writes this for your columns, then runs it on your data before you copy it. Open the tool ↗

Bring us the problem. We'll work through the system.

Your next
hard problem.↗