summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/lsqlite3/examples/aggregate.lua
blob: 6087b0c65ccd175b3bbf0eeb86fb4a35a7bca828 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
local sqlite3 = require("lsqlite3")

local db = sqlite3.open_memory()

assert( db:exec "CREATE TABLE test (col1, col2)" )
assert( db:exec "INSERT INTO test VALUES (1, 2)" )
assert( db:exec "INSERT INTO test VALUES (2, 4)" )
assert( db:exec "INSERT INTO test VALUES (3, 6)" )
assert( db:exec "INSERT INTO test VALUES (4, 8)" )
assert( db:exec "INSERT INTO test VALUES (5, 10)" )

do

  local square_error_sum = 0

  local function step(ctx, a, b)
    local error        = a - b
    local square_error = error * error
    square_error_sum   = square_error_sum + square_error
  end

  local function final(ctx)
    ctx:result_number( square_error_sum / ctx:aggregate_count() )
  end

  assert( db:create_aggregate("my_stats", 2, step, final) )

end

--for a,b in db:urows("SELECT col1, col2 FROM test")
--do print("a b: ", a, b) end

for my_stats in db:urows("SELECT my_stats(col1, col2) FROM test")
do print("my_stats:", my_stats) end