Skip to content

Instantly share code, notes, and snippets.

@jylopez
Created April 5, 2017 22:43
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 jylopez/be2c31e793bad6a636ac6d3a2bc90b24 to your computer and use it in GitHub Desktop.
Save jylopez/be2c31e793bad6a636ac6d3a2bc90b24 to your computer and use it in GitHub Desktop.
Postgres example distinct
WITH cohorts_join AS (SELECT
cohorts.id,
cohorts.name,
cohorts.settings,
cohorts.created_at,
cohorts.updated_at,
registrations.product_id,
products.type AS product_type
FROM cohorts, cohorts_registrations, registrations, products
WHERE
cohorts.id = cohorts_registrations.cohort_id
AND cohorts_registrations.registration_id = registrations.id
AND registrations.product_id = products.id
)
SELECT DISTINCT ON (id, product_id) * FROM cohorts_join;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment