Thanks for your suggestion,
indeed it is called for each row. That's my problem. A better solution
which just extract the last result is:
package require sqlite
sqlite sql d:/data/yaspo2004bt.sqlite
sql function median Median
set l [list]
proc Median {value} {
global l
lappend l $value
return [lindex [lsort -integer $l] [expr round([llength $l] / 2)]]
# not absolute exact but just a draft
}
puts [lindex {sql eval "select median(Size) from tensembl"} end]
However I want to execute it as a true aggregate function which should
only return 1 row. I saw that this seems possible with php
(http://www.klevze.si/php_manual/function.sqlite-create-aggregate.php)
May be I must extend tclsqlite.c with an enum DB_AGGREGATE and an case
DB_AGGREGATE which adjusts this part:
// partof tclsqlite.c
case DB_FUNCTION: {
SqlFunc *pFunc;
char *zName;
char *zScript;
int nScript;
if( objc!=4 ){
Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
return TCL_ERROR;
}
zName = Tcl_GetStringFromObj(objv[2], 0);
zScript = Tcl_GetStringFromObj(objv[3], &nScript);
pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
if( pFunc==0 ) return TCL_ERROR;
pFunc->interp = interp;
pFunc->pNext = pDb->pFunc;
pFunc->zScript = (char*)&pFunc[1];
strcpy(pFunc->zScript, zScript);
sqlite_create_function(pDb->db, zName, -1, tclSqlFunc, pFunc);
sqlite_function_type(pDb->db, zName, SQLITE_NUMERIC);
break;
}
Your idea with the median is right and faster than my one but I want to
implement a true sql-solution.
Anyone with more c-knowledge than me to implement this ?
> >Hello,
> >
> >Is it possible to write the median as an aggregate function using pure
> >tcl.
>
> No, there does not seem to be a way of specifying that a Tcl function in
> SQLite should be an aggregate function, not a normal one (based on a quick
> look through the tclsqlite.c file).
>
> Two further comments:
>
> 1. Your Median function seems to be expecting to be called once for each
> row, rather than just once total (or else how will the lappend part work?)
>
> 2. Won't something like the following do what you want?
>
> set sortedlist [lsort -integer [sql eval {select Size from tensembl}]]
> lindex $sortedlist [expr round([llength $sortedlist] / 2)]]
>
> Gerry
>
>
regards,
Detlef
--
Dr. Detlef Groth
Max-Planck-Institut
fuer Molekulare Genetik
Ihnestr. 63/73
D-14195 Berlin
Tel.: + 49 30 - 8413 1235
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]