- How do I group numbers into ranges (buckets) in SQL?
- Ask in plain English — 'count of orders bucketed by amount: under 50, 50 to 200, over 200.' nlqdb compiles a `CASE WHEN amount < 50 THEN ... END` bucket label, groups by that label instead of the raw amount, and orders the ranges by their lower bound. It runs the query in Postgres and shows the SQL, so you can confirm the bands tile without a gap.
- Why does GROUP BY give me one row per value instead of per range?
- Because `GROUP BY amount` groups by the exact value, so every distinct amount is its own group. To get bands you group by a derived bucket — a `CASE` expression that maps each amount to a range label (or `width_bucket` for even-width bins) — so all amounts in the same band collapse into one row.
- Why do my range buckets sort 10-20 before 5-10?
- Because the bucket labels are text, and `ORDER BY bucket` sorts text alphabetically — '1' sorts before '5', so '10-20' lands before '5-10'. Order by the numeric lower bound of each band (or by the `CASE`/`width_bucket` index) instead of the label. nlqdb compiles the numeric ordering and shows it in the SQL.
- Can I bucket numbers on a Postgres database I already run?
- Yes — connect it with the signed-in BYO connect verb (`nlq db connect`, `SK-DBCONN-001`; see /solve/query-existing-postgres-in-natural-language) and ask for the histogram in place, no ETL into a separate store. The honest limits: BYO connect is signed-in only (not the public embed), and the answer is read-only — nlqdb reports the buckets, it doesn't materialise them into a table.