I'll like to find Nth percentile.
for example: table: htwt; columns: name, gender, height, weight
result:
| gender | 90% height | 90% weight |
| male | 190 | 90 |
| female | 180 | 80 |
sqlite is not strong in analytical processing but if your data is not very large, you can try to emulate percentile with ORDER BY
, LIMIT 1
and a calculated OFFSET
. Note that OFFSET
is zero-based so you need to adjust it by one.
SELECT
height AS 'male 90% height'
FROM table
WHERE gender='male'
ORDER BY height ASC
LIMIT 1
OFFSET (SELECT
COUNT(*)
FROM table
WHERE gender='male') * 9 / 10 - 1;
I needed multiple percentages (10, 20 ... 100%) and solved it with:
WITH p AS (SELECT height, NTILE(10) OVER (ORDER BY height) AS percentile
FROM table
WHERE gender = 'male')
SELECT percentile, MAX(height) as height
FROM p
GROUP BY percentile;
This solution requires SQLite 3.28.0 or later for the NTILE window function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With