SQLite running sum side effect

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

SQLite running sum side effect

petern
If you want a controlled side effect, like a running sum, add your own
stateful extension functions.  Example:

static double g_sum;
static void FloatSumReset(sqlite3_context *context, int argc, sqlite3_value
**argv) {
  g_sum = sqlite3_value_double(argv[0]);
}
static void FloatSumAccum(sqlite3_context *context, int argc, sqlite3_value
**argv) {
  g_sum += sqlite3_value_double(argv[0]);
  sqlite3_result_double(context,g_sum);
}

Then, in SQLite:

sqlite> WITH num_list(x) AS (VALUES (1),(2),(3)), reset AS (SELECT
FloatSumReset(0)) SELECT x,FloatSumAccum(x) FROM reset,num_list;
x,FloatSumAccum(x)
1,1.0
2,3.0
3,6.0
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users