标题:[原创]扩展求方差的mysql函数例子
只看楼主
antter
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2005-8-27
 问题点数:0 回复次数:1 
[原创]扩展求方差的mysql函数例子

源码
// 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>

如果有建议或疑问欢迎留言
我的blog是http://blog.sina.com.cn/u/1259926384

搜索更多相关主题的帖子: 方差 函数 mysql 例子 
2006-10-20 00:42
ss123456789
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-11-9
得分:0 

不错~

2006-11-09 15:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-97485-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.013550 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved