本页还没有译文
本页 9 段还没译成简体中文,下面显示的是英文原文。Select first row for each group in Postgres
还没翻译译自英文(supabase.com)本页 9 段都还没译
Given a table seasons:
| id | team | points |
|---|---|---|
| 1 | Liverpool | 82 |
| 2 | Liverpool | 84 |
| 3 | Brighton | 34 |
| 4 | Brighton | 28 |
| 5 | Liverpool | 79 |
We want to find the rows containing the maximum number of points per team.
The expected output we want is:
| id | team | points |
|---|---|---|
| 3 | Brighton | 34 |
| 2 | Liverpool | 84 |
Run the following SQL using the SQL Editor in the Supabase Dashboard or with psql over a direct connection. To connect with psql, see Direct connection.
select distinct
on (team) id,
team,
points
from
seasons
order by
team,
points desc;The important bits here are:
- The
desckeyword to order thepointsfrom highest to lowest. - The
distinctkeyword that tells Postgres to only return a single row per team.
AI 翻译查看英文原文