Skip to content

Instantly share code, notes, and snippets.

@den-crane
Created January 3, 2023 14:56
Show Gist options
  • Save den-crane/a493a415105daffa236e3ca69f983075 to your computer and use it in GitHub Desktop.
Save den-crane/a493a415105daffa236e3ca69f983075 to your computer and use it in GitHub Desktop.
WITH table AS ( SELECT cityHash64(number) AS value from numbers(100000000)),
(SELECT quantilesExactInclusive(0.25, 0.5, 1)(value) FROM table) AS dist
select upper, lower, count() from (
SELECT arrayLastIndex(i -> (i < value), dist) AS index,
dist[least(index, length(dist)-1)] lower,
dist[least(index+1, length(dist))] upper
FROM table)
group by upper, lower
order by upper, lower
┌────────────────upper─┬───────────────lower─┬──count()─┐
│ 4611344705468642300 │ 0 │ 25000000 │
│ 9222829123983542000 │ 4611344705468642300 │ 25000000 │
│ 14532581387030411000 │ 9222829123983542000 │ 50000000 │
└──────────────────────┴─────────────────────┴──────────┘
3 rows in set. Elapsed: 7.300 sec. Processed 200.02 million rows, 1.60 GB (27.40 million rows/s., 219.21 MB/s.)
Peak memory usage (for query): 1.25 GiB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment