WIDTH_BUCKET
Tells you which of several equal ranges a number falls into: 1, 2, 3…
Syntax
WIDTH_BUCKET(<value>, <from>, <to>, <bucket count>)
WIDTH_BUCKET(<value>, ARRAY[<bound1>, <bound2>, …])
Splits the interval from <from> to <to> into the given number of equal buckets and returns the bucket number for the value. Anything below the start gets 0, and anything not below the end gets the count + 1. Handy for histograms: how many orders are under 50, from 50 to 100 and so on.