The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: adds distinct row select helper
awalias25acbae69400
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex bd86249c517..caa4bc52d5e 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -616,6 +616,7 @@ export const resources = {url: undefined,items: [{ name: 'Drop all tables in schema', url: '/guides/resources/postgres/dropping-all-tables-in-schema' },+ { name: 'Select first row per group', url: '/guides/resources/postgres/first-row-in-group' },{ name: 'Print PostgreSQL version', url: '/guides/resources/postgres/which-version-of-postgres' },],},diff --git a/apps/docs/pages/guides/resources/postgres/first-row-in-group.mdx b/apps/docs/pages/guides/resources/postgres/first-row-in-group.mdxnew file mode 100644index 00000000000..0810879a962--- /dev/null+++ b/apps/docs/pages/guides/resources/postgres/first-row-in-group.mdx@@ -0,0 +1,43 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ title: 'Select first row for each group in PostgreSQL',+ description:+ 'PostgreSQL snippet for grabbing the first row in each distinct group by group',+}++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](https://app.supabase.com/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` that we put on the ordering of `points` which ranks the points in descending value (starting with the highest). And 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).++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Page