SQLite Forum

Get column rank from values in selected row
Login
I agree with Gunter's post - this should really be fixed in the INPUT phase, which will future proof your queries.

If you still only want to fix it as it is now, I suggest perhaps changing my suggestion, considering Gunter's comments, to be a view that can be queried in lots of places, but fixed in one place, should the need arise:

```
CREATE TABLE centiles(age, c10, c25, c50, c75, c90);
INSERT INTO centiles VALUES 
 ( 1, 5, 6, 7,  8,  9)
,( 2, 6, 7, 8,  9, 10)
,( 3, 7, 8, 9, 10, 11)
;


CREATE VIEW centiles_normed AS
  SELECT 0 AS age, 0 AS weight, 0 AS percentile UNION ALL
  SELECT age, c10, 10 FROM centiles UNION ALL
  SELECT age, c25, 25 FROM centiles UNION ALL
  SELECT age, c50, 50 FROM centiles UNION ALL
  SELECT age, c75, 75 FROM centiles UNION ALL
  SELECT age, c90, 90 FROM centiles 
;

(Note: The first select simply adds the column names, but you can do that with an actual row too if you wish not to have a Zero age row)


To later get the specific percentiles, you can have queries like this:

SELECT percentile
  FROM centiles_normed
 WHERE age = 3 AND weight = 9
;
```