jeudi 9 avril 2015

Sum of Avg of Groups in SQLite

Simple table "test":



Name Value
A 2
A 3
B 2
B 3
B 5
C 7


With a simple query I can get the following result:


select Name, count(*), sum(Value), avg(Value) from test group by Name



Count Sum Avg
A 2 5 2,50
B 3 10 3,33
C 1 7 7,00


The total count, sum and avg are easy, too:


select count(*), sum(Value), avg(Value) from test



6 22 3,67


But how to query the sum of avg?


12,83 = 2,5 + 3,33 + 7,00


Needed result:



Count Sum Avg Avg / 12,83 * 100
A 2 5 2,50 19% = 2,50 / 12,83 * 100
B 3 10 3,33 26%
C 1 7 7,00 55%

Aucun commentaire:

Enregistrer un commentaire