stats

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stats gives SQLite the variance and the standard deviation as aggregate functions:

sqlite.NewConnector(dsn, stats.Option())

Then var_samp(x), var_pop(x), stddev_samp(x) and stddev_pop(x), with stddev and variance as other names of the sample ones, as in PostgreSQL. NULLs are skipped. The sample ones give NULL for fewer than 2 values, the population ones for none. A text or a blob is an error.

The result is within a few units in the last place of the exact one (Welford's one-pass method, akin to what PostgreSQL and DuckDB use), not always the nearest float64. Large values with a small spread keep their digits, integers past 2^53 too, and values near the ends of a float64 neither overflow nor underflow on the way. A result whose computed value passes the largest float64 is an error, as in PostgreSQL. The standard deviation of values whose variance does not fit still comes back. Within a few units in the last place of the largest float64, the exact result may come back as the error, and one just past it as the largest float64.

Example
package main

import (
	"database/sql"
	"fmt"

	"github.com/liustack/sqlite"
	"github.com/liustack/sqlite/ext/stats"
)

func main() {
	c, err := sqlite.NewConnector(":memory:", stats.Option())
	if err != nil {
		panic(err)
	}
	db := sql.OpenDB(c)
	defer db.Close()
	var samp, pop float64
	err = db.QueryRow(`SELECT stddev(column1), stddev_pop(column1) FROM (VALUES (2), (4), (4), (4), (5), (5), (7), (9))`).Scan(&samp, &pop)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%.4f %.4f\n", samp, pop)
}
Output:
2.1381 2.0000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Option

func Option() sqlite.Option

Option adds the functions to a connector.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL