Standard Deviation for SQLite

Viewed 35171

I've searched the SQLite docs and couldn't find anything, but I've also searched on Google and a few results appeared.

Does SQLite have any built-in Standard Deviation function?

10 Answers

You can calculate the variance in SQL:

create table t (row int);
insert into t values (1),(2),(3);
SELECT AVG((t.row - sub.a) * (t.row - sub.a)) as var from t, 
    (SELECT AVG(row) AS a FROM t) AS sub;
0.666666666666667

However, you still have to calculate the square root to get the standard deviation.

Use variance formula V(X) = E(X^2) - E(X)^2. In SQL sqlite

SELECT AVG(col*col) - AVG(col)*AVG(col) FROM table

To get standard deviation you need to take the square root V(X)^(1/2)

No, I searched this same issue, and ended having to do the calculations with my application (PHP)

You don't state which version of standard deviation you wish to calculate but variances (standard deviation squared) for either version can be calculated using a combination of the sum() and count() aggregate functions.

select  
(count(val)*sum(val*val) - (sum(val)*sum(val)))/((count(val)-1)*(count(val))) as sample_variance,
(count(val)*sum(val*val) - (sum(val)*sum(val)))/((count(val))*(count(val))) as population_variance
from ... ;

It will still be necessary to take the square root of these to obtain the standard deviation.

Related