Skip to content

Instantly share code, notes, and snippets.

@danielfone
Created April 28, 2020 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielfone/b3fc6e1e8028cc83b38722c78ca1367e to your computer and use it in GitHub Desktop.
Save danielfone/b3fc6e1e8028cc83b38722c78ca1367e to your computer and use it in GitHub Desktop.
create table enrolments (
id serial PRIMARY KEY,
organisation_name text
)
-- Let's say we want to partition our table into pages of 50 rows
-- and sorted alphabetically.
with
dist as (
select
organisation_name,
id,
row_number() over (order by organisation_name, id) as row_number
from enrolments
)
-- This will return the record at the 'top' of each page
-- the name+id serves as a natural key to use when fetching the page
-- We can construct our page links with this result
select row_number, organisation_name, id
from dist
where mod(row_number-1, 50) = 0
;
-- query to get the rows
select id, organisation_name
from enrolments
where organisation_name >= 'Acme Inc' and id >= 75
order by organisation_name, id
limit 50
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment