Standard Deviation for SQLite

后端 未结 10 672
生来不讨喜
生来不讨喜 2020-12-14 08:05

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 Devi

相关标签:
10条回答
  • 2020-12-14 08:28

    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.

    0 讨论(0)
  • 2020-12-14 08:29

    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)

    0 讨论(0)
  • 2020-12-14 08:29

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

    0 讨论(0)
  • 2020-12-14 08:32

    a little trick

    select ((sum(value)*sum(value) - sum(value * value))/((count(*)-1)*(count(*)))) 
    from the_table ;
    

    then the only thing left is to calculate sqrt outside.

    0 讨论(0)
  • 2020-12-14 08:33

    The aggregate functions supported by SQLite are here:

    http://www.sqlite.org/lang_aggfunc.html

    STDEV is not in the list.

    However, the module extension-functions.c in this page contains a STDEV function.

    0 讨论(0)
  • 2020-12-14 08:37

    I implemented the Welford's method (the same as extension-functions.c) as a SQLite UDF:

    $db->sqliteCreateAggregate('stdev',
        function (&$context, $row, $data) // step callback
        {
            if (isset($context) !== true) // $context is null at first
            {
                $context = array
                (
                    'k' => 0,
                    'm' => 0,
                    's' => 0,
                );
            }
    
            if (isset($data) === true) // the standard is non-NULL values only
            {
                $context['s'] += ($data - $context['m']) * ($data - ($context['m'] += ($data - $context['m']) / ++$context['k']));
            }
    
            return $context;
        },
        function (&$context, $row) // fini callback
        {
            if ($context['k'] > 0) // return NULL if no non-NULL values exist
            {
                return sqrt($context['s'] / $context['k']);
            }
    
            return null;
        },
    1);
    

    That's in PHP ($db is the PDO object) but it should be trivial to port to another language.

    SQLite is soooo cool. <3

    0 讨论(0)
提交回复
热议问题