Skip to content

Instantly share code, notes, and snippets.

@mble
Created June 3, 2017 12:06
Show Gist options
  • Save mble/2a3e410c35b90b8da2a2d945a964d949 to your computer and use it in GitHub Desktop.
Save mble/2a3e410c35b90b8da2a2d945a964d949 to your computer and use it in GitHub Desktop.
Calculating the min, median and max in PostgreSQL
-- Small example demonstrating to calculate the median without using an custom built aggregate function
select
min(score)
-- percentile_cont is a function that returns a value corresponding to the specified fraction in the ordering,
-- interpolating between adjacent input items if needed. By setting the fraction to 0.5, this essentially works
-- as the median of the ordered set provided by the grouping.
, percentile_cont(0.5) with group (order by score) AS median
, max(score)
from result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment