1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-10-24 09:53:10 +03:00

Add support for the TOTAL() aggregate function - works like SUM() except

that it returns 0 instead of NULL when presented with an empty list. (CVS 2930)

FossilOrigin-Name: a7f528ff3446d50b280fb0b85063879e3ac5751a
This commit is contained in:
drh
2006-01-12 22:17:50 +00:00
parent 0e6082eb83
commit a97fdd3bfc
5 changed files with 39 additions and 19 deletions

View File

@@ -16,7 +16,7 @@
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.115 2006/01/09 16:12:05 danielk1977 Exp $
** $Id: func.c,v 1.116 2006/01/12 22:17:50 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -823,7 +823,13 @@ struct SumCtx {
};
/*
** Routines used to compute the sum or average.
** Routines used to compute the sum, average, and total.
**
** The SUM() function follows the (broken) SQL standard which means
** that it returns NULL if it sums over no inputs. TOTAL returns
** 0.0 in that case. In addition, TOTAL always returns a float where
** SUM might return an integer if it never encounters a floating point
** value.
*/
static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
SumCtx *p;
@@ -857,6 +863,11 @@ static void avgFinalize(sqlite3_context *context){
sqlite3_result_double(context, p->sum/(double)p->cnt);
}
}
static void totalFinalize(sqlite3_context *context){
SumCtx *p;
p = sqlite3_aggregate_context(context, 0);
sqlite3_result_double(context, p ? p->sum : 0.0);
}
/*
** An instance of the following structure holds the context of a
@@ -1000,6 +1011,7 @@ void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
{ "min", 1, 0, 1, minmaxStep, minMaxFinalize },
{ "max", 1, 2, 1, minmaxStep, minMaxFinalize },
{ "sum", 1, 0, 0, sumStep, sumFinalize },
{ "total", 1, 0, 0, sumStep, totalFinalize },
{ "avg", 1, 0, 0, sumStep, avgFinalize },
{ "count", 0, 0, 0, countStep, countFinalize },
{ "count", 1, 0, 0, countStep, countFinalize },