mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 04:15:04 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Updates verbiage throughout docs to use postgres over postgresql. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated terminology throughout documentation, guides, and resources for consistent product naming across all user-facing materials, including page titles, descriptions, and reference documentation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
47 lines
1.2 KiB
Plaintext
47 lines
1.2 KiB
Plaintext
---
|
|
title: 'Select first row for each group in Postgres'
|
|
description: 'Postgres snippet for grabbing the first row in each distinct group by group'
|
|
footerHelpType: 'postgres'
|
|
---
|
|
|
|
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 |
|
|
|
|
From the [SQL Editor](/dashboard/project/_/sql), you can run a query like:
|
|
|
|
```sql
|
|
select distinct
|
|
on (team) id,
|
|
team,
|
|
points
|
|
from
|
|
seasons
|
|
order BY
|
|
id,
|
|
points desc,
|
|
team;
|
|
```
|
|
|
|
The important bits here are:
|
|
|
|
- The `desc` keyword to order the `points` from highest to lowest.
|
|
- The `distinct` keyword that tells Postgres to only return a single row per team.
|
|
|
|
This query can also be executed via `psql` or any other query editor if you prefer to [connect directly to the database](/docs/guides/database/connecting-to-postgres#direct-connections).
|