博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql server 2012 自定义聚合函数(MAX_O3_8HOUR_ND) 计算最大的臭氧8小时滑动平均值
阅读量:4327 次
发布时间:2019-06-06

本文共 3175 字,大约阅读时间需要 10 分钟。

采用c#开发dll,并添加到sql server 中。

具体代码,可以用visual studio的向导生成模板。

using System;using System.Collections;using System.Data;using Microsoft.SqlServer.Server;using System.Data.SqlTypes;using System.IO;using System.Text;[Serializable][Microsoft.SqlServer.Server.SqlUserDefinedAggregate(    Format.UserDefined, //use clr serialization to serialize the intermediate result    IsInvariantToNulls = true, //optimizer property    IsInvariantToDuplicates = false, //optimizer property    IsInvariantToOrder = false, //optimizer property    MaxByteSize = 8000) //maximum size in bytes of persisted value]public class MAX_O3_8HOUR_ND : IBinarySerialize{    ///     /// The variable that holds the intermediate result of the concatenation    ///     private StringBuilder intermediateResult;    ///     /// 系统初始化    ///     public void Init()    {        this.intermediateResult = new StringBuilder();    }    ///     /// 积累文本内容,null除外,一般用标点符号隔开。    ///     ///     public void Accumulate(SqlString value)    {        if (value.IsNull)        {            return;        }        this.intermediateResult.Append(value.Value).Append(',');    }    ///     /// Merge the partially computed aggregate with this aggregate.    ///     ///     public void Merge(MAX_O3_8HOUR_ND Group)    {        this.intermediateResult.Append(Group.intermediateResult);    }    ///     ///在最后被调用,返回聚合函数结果    ///     /// 
public SqlString Terminate() { string output = string.Empty; ArrayList list = new ArrayList(); if (this.intermediateResult != null&& this.intermediateResult.Length > 0) { output = this.intermediateResult.ToString(0, this.intermediateResult.Length - 1); string [] result=output.Split(','); float max = 0; if (result.Length >= 8) { for (int i = 0; i <= result.Length - 8; i++) { float re = 0; for (int j = i; j < 8 + i; j++) { re = re + Convert.ToSingle(result[j]); } re=re/8; if (re > max) { max = re; } } output = Math.Ceiling(max).ToString(); } else { output=string.Empty; } } return new SqlString(output); } public void Read(BinaryReader r) { intermediateResult = new StringBuilder(r.ReadString()); } public void Write(BinaryWriter w) { w.Write(this.intermediateResult.ToString()); }}

dll添加到sql server,创建聚合函数。

CREATE ASSEMBLY [MAX_O3_8HOUR_ND] AUTHORIZATION [dbo]FROM 'c:\MAX_O3_8HOUR_ND.dll'WITH PERMISSION_SET = SAFE;CREATE AGGREGATE [dbo].[MAX_O3_8HOUR_ND] (@FieldValue [nvarchar](4000))RETURNS [nvarchar](4000)EXTERNAL NAME [MAX_O3_8HOUR_ND].[MAX_O3_8HOUR_ND];

sql server 开启 CLR支持:

EXEC sp_configure 'clr enabled', 1RECONFIGURE WITH OVERRIDEGO

 

示例:

SELECT FDATE,SITENAME, dbo.MAX_O3_8HOUR_ND(O3)

FROM (select top 100 percent * from MONITOR_ND order by ftime)a
GROUP BY SITENAME,FDATE

转载于:https://www.cnblogs.com/tiandi/p/5605130.html

你可能感兴趣的文章
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>