SQLite Forum

Get column rank from values in selected row
Login
All sorted now and although this has little to do with SQLite now, I might as well show how this was done. This is coding in B4A, for Android phones.
As it turns out I only need this clinical data:

Sex	Agemos	M	S
------------------------------------------
1	0	3.530203168	0.152385273
1	1	4.003106424	0.146025021
1	2	4.879525083	0.136478767
1	3	5.672888765	0.129677511
1	4	6.391391982	0.124717085
1	5	7.041836432	0.121040119
1	6	7.630425182	0.1182712
1	7	8.162951035	0.116153695
1	8	8.644832479	0.114510349
1	9	9.081119817	0.113217163
1	10	9.476500305	0.11218624
1	11	9.835307701	0.111354536
1	12	10.16153567	0.110676413
1	13	10.45885399	0.110118635
1	14	10.7306256	0.109656941
1	15	10.97992482	0.109273653
1	16	11.20955529	0.10895596
1	17	11.4220677	0.108694678
1	18	11.61977698	0.108483324
1	19	11.80477902	0.108317416
1	20	11.9789663	0.108193944
1	21	12.14404334	0.108110954
1	22	12.30154103	0.108067236
1	23	12.45283028	0.108062078
1	24	12.59913494	0.108095077
1	25	12.74154396	0.108166005
1	26	12.88102276	0.108274705
1	27	13.01842382	0.108421024
1	28	13.1544966	0.108604769
1	29	13.28989667	0.108825681
1	30	13.42519408	0.109083423
1	31	13.56088113	0.109377581
1	32	13.69737858	0.109707646
1	33	13.83504622	0.110073084
1	34	13.97418199	0.110473238
1	35	14.1150324	0.1109074
1	36	14.25779618	0.111374787
1	37	14.32994444	0.111620652
2	0	3.39918645	0.142106724
2	1	3.79752846	0.138075916
2	2	4.544776513	0.131733888
2	3	5.230584214	0.126892697
2	4	5.859960798	0.123025182
2	5	6.437587751	0.119840911
2	6	6.967850457	0.117166868
2	7	7.454854109	0.11489384
2	8	7.902436186	0.112949644
2	9	8.314178377	0.11128469
2	10	8.693418423	0.109863709
2	11	9.043261854	0.10866078
2	12	9.366593571	0.10765621
2	13	9.666089185	0.106834517
2	14	9.944226063	0.106183085
2	15	10.20329397	0.105691242
2	16	10.4454058	0.105349631
2	17	10.67250698	0.105149754
2	18	10.88638558	0.105083666
2	19	11.08868151	0.105143752
2	20	11.28089537	0.105322575
2	21	11.46439708	0.10561278
2	22	11.64043402	0.106007025
2	23	11.81013895	0.106497957
2	24	11.97453748	0.107078197
2	25	12.13455528	0.107740346
2	26	12.2910249	0.108477009
2	27	12.44469237	0.109280822
2	28	12.59622335	0.110144488
2	29	12.74620911	0.111060814
2	30	12.89517218	0.112022758
2	31	13.04357164	0.113023466
2	32	13.19180827	0.114056316
2	33	13.34022934	0.115114952
2	34	13.48913357	0.116193337
2	35	13.63877446	0.11728575
2	36	13.78936547	0.118386847
2	37	13.86507382	0.118939087

And then a conversion table to convert Z-Score to centile

Z_Score,Centile
----------------
-2.326,1
-2.054,2
-1.881,3
-1.751,4
-1.645,5
-1.555,6
-1.476,7
-1.405,8
-1.341,9
-1.282,10
-1.227,11
-1.175,12
-1.126,13
-1.08,14
-1.036,15
-0.994,16
-0.954,17
-0.915,18
-0.878,19
-0.842,20
-0.806,21
-0.772,22
-0.739,23
-0.706,24
-0.674,25
-0.643,26
-0.613,27
-0.583,28
-0.553,29
-0.524,30
-0.496,31
-0.468,32
-0.44,33
-0.412,34
-0.385,35
-0.358,36
-0.332,37
-0.305,38
-0.279,39
-0.253,40
-0.228,41
-0.202,42
-0.176,43
-0.151,44
-0.126,45
-0.1,46
-0.075,47
-0.05,48
-0.025,49
0,50
0.025,51
0.05,52
0.075,53
0.1,54
0.126,55
0.151,56
0.176,57
0.202,58
0.228,59
0.253,60
0.279,61
0.305,62
0.332,63
0.358,64
0.385,65
0.412,66
0.44,67
0.468,68
0.496,69
0.524,70
0.553,71
0.583,72
0.613,73
0.643,74
0.674,75
0.706,76
0.739,77
0.772,78
0.806,79
0.842,80
0.878,81
0.915,82
0.954,83
0.994,84
1.036,85
1.08,86
1.126,87
1.175,88
1.227,89
1.282,90
1.341,91
1.405,92
1.476,93
1.555,94
1.645,95
1.751,96
1.881,97
2.054,98
2.326,99

B4A code is then like this:

Sub btnCentilesCalculate_Click
	
	Dim iSexType As Int
	Dim iMonths As Int
	Dim dWeight As Double
	Dim dMedian As Double
	Dim strSQL As String
	Dim dGCV As Double 'Generalized Coefficient of Variation
	Dim dZ As Double 'Z-Score
	Dim iCentile As Int
	Dim RS1 As ResultSet
	
	If chkCentilesMale.Checked Then
		iSexType = 1
	Else
		iSexType = 2
	End If
	
	iMonths = edtCentilesMonths.Text
	dWeight = edtCentilesWeight.Text
	
	strSQL = "select M, S from weight_data_m36 " & _
			 "where sex = ? And agemos = ?"
	RS1 = General.cConn.SQL1.ExecQuery2(strSQL, _
	Array As String(iSexType, iMonths))
	RS1.Position = 0
	dMedian = RS1.GetDouble2(0)
	dGCV = RS1.GetDouble2(1)
	
	dZ = Logarithm(dWeight / dMedian, cE) / dGCV
	strSQL = "select centile from zscore2centile " & _
			 "order by Abs(? - z_score) Asc limit 1"
	iCentile = General.cConn.SQL1.ExecQuerySingleResult2(strSQL, _
			   Array As String(dZ))
	
	lblCentile.Text = iCentile

End Sub

RBS