SQLite Forum

Get column rank from values in selected row
Login
Well, that's not how SQL works, but, in the interest of fun....

Are the tables ALWAYS the same layout? (i.e. have the exact same columns?)

If that is the case, then one can do something like this:

```
WITH normalized(age, weight, percentile) AS (
    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 
)
SELECT *
  FROM normalized
;


-- And to get the specific percentile for Age 3 at weight 9 - simply chenge the end query to 

SELECT percentile
  FROM normalized
 WHERE age = 3 AND weight = 9
;


EDIT: I should add that the table "centiles" being used is constructed from the OP's given layout - resulting in:

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)
;
```

This will work for any size table and can be adjusted to suit more columns.