Puoi creare un elenco di intervalli utilizzando UNION ALL
e LEFT JOIN
con esso:
SELECT CONCAT(IFNULL(ranges.min, '∞'), '-', IFNULL(ranges.max, '∞')) AS `range`, avg(value) AS avg
FROM (
SELECT 0 AS min, 21 AS max UNION ALL
SELECT 21, 34 UNION ALL
SELECT 34, 64 UNION ALL
SELECT 64, NULL
) AS ranges
LEFT JOIN t ON (ranges.min IS NULL OR value >= ranges.min) AND
(ranges.max IS NULL OR value < ranges.max)
GROUP BY ranges.min, ranges.max
Nota che la query precedente inserirà 20.9999
dentro [0-21)
e 21.0000
dentro [21-34)
intervallo.