扩展求方差的mysql函数例子
2008-02-23 07:40:28来源:互联网 阅读 ()
源码
// Author: JiangMiao // Name: 扩展求方差的MySQL函数列子 // Date: 2006-10-19 // Link: http://blog.sina.com.cn/u/1259926384 - JiangMiao的Blog #include "winsock2.h" #include "MySQL.h" #include <vector> using namespace std; #define SAFE_DELETE(p) if(p!=NULL){delete p;p=NULL;} #define CDLLEXPORT extern "C" __declspec(dllexport) typedef __int64 longlong; typedef vector<double> vec_double; typedef unsigned long ulong; class VAR { private: vec_double datas; double total; public: VAR():total(0.0) {} //加入num void push_back(double num) { datas.push_back(num); total =num; } void clear() { datas.clear(); total=0.0; } //取方差 double getVariance() { size_t count=datas.size(); double avr=0.0; avr=(total/count); //平均数 double rt=0.0; for(size_t i=0;i<count;i ) { double k=(datas[i]-avr); rt =k*k; } return rt/count; }
}; CDLLEXPORT my_bool variance_init(UDF_INIT *initid, UDF_ARGS *args, char *message) { initid->ptr = NULL; if(args->arg_count!=1) //参数个数为1 { return 1; } if(args->arg_type[0]!=REAL_RESULT||args->arg_type[0]!=INT_RESULT) //参数类别为整型或double { return 1; } initid->ptr = (char*)new VAR(); return 0; } CDLLEXPORT void variance_deinit(UDF_INIT *initid) { VAR* ptr=(VAR*)initid->ptr; delete ptr; } CDLLEXPORT double variance(UDF_INIT *initid, UDF_ARGS *args,char *is_null, char *error) { VAR* ptr=(VAR*)initid->ptr; return ptr->getVariance(); } CDLLEXPORT void variance_clear(UDF_INIT *initid, char *is_null, char *error) { VAR* ptr=(VAR*)initid->ptr; ptr->clear(); } CDLLEXPORT void variance_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { VAR* ptr=(VAR*)initid->ptr; char* argo=args->args[0]; double arg; if(args->arg_type[0]==REAL_RESULT) { arg=*(double*)argo; } if(args->arg_type[0]==INT_RESULT) { arg=(double)*(__int64*)argo; } ptr->push_back(arg); }
|
编译后得到variance.dll
复制到bin目录下
测试
MySQL> use test; Database changed MySQL> create table vartest (realtest real,inttest int); Query OK, 0 rows affected (0.11 sec)
MySQL> insert into vartest values(5,5),(6,6),(9,9),(10,10),(5,5); Query OK, 5 rows affected (0.03 sec) Records: 5 Duplicates: 0 Warnings: 0
MySQL> create aggregate function variance returns real soname 'variance.dll'; Query OK, 0 rows affected (0.00 sec)
MySQL> select variance(realtest),variance(inttest) from vartest; -------------------- ------------------- | variance(realtest) | variance(inttest) | -------------------- ------------------- | 4.4 | 4.4000 | -------------------- ------------------- 1 row in set (0.00 sec)
MySQL>
|
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有