0

Other databases have good functions for histogram... For example Hive offers histogram_numeric(numeric_column,b), where b is the "number non-uniformly spaced bins".

Is there a library for PostgreSQL that offers a similar function for numeric columns?

Important: this Wiki function is ugly, is not simple.

1
  • Are you looking for width_bucket? Commented Mar 23, 2021 at 21:30

1 Answer 1

3

PostgreSQL is better! It has the ntile window function:

SELECT n, ntile(3) OVER (ORDER BY n)
FROM generate_series(1, 20) AS n;

 n  | ntile 
----+-------
  1 |     1
  2 |     1
  3 |     1
  4 |     1
  5 |     1
  6 |     1
  7 |     1
  8 |     2
  9 |     2
 10 |     2
 11 |     2
 12 |     2
 13 |     2
 14 |     2
 15 |     3
 16 |     3
 17 |     3
 18 |     3
 19 |     3
 20 |     3
(20 rows)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.