mysql中的方差函数_[原创]扩展求方差的mysql函数例子

源码

//

Author: JiangMiao

//

Name: 扩展求方差的mysql函数列子

//

Date: 2006-10-19

//

Link:

http://blog.csdn.net/antter

- 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>

如果有建议或疑问欢迎留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值