SQL Server 2012 custom aggregate function (MAX_O3_8HOUR_ND) calculates the maximum 8-hour sliding average of ozone

Source: Internet
Author: User

Develop DLLs in C # and add them to SQL Server.

Specific code, you can use the Wizard of Visual Studio to generate the template.

usingSystem;usingSystem.Collections;usingSystem.Data;usingMicrosoft.SqlServer.Server;usingSystem.Data.SqlTypes;usingSystem.IO;usingSystem.Text; [Serializable] [Microsoft.SqlServer.Server.SqlUserDefinedAggregate (format.userdefined,//Use of CLR serialization to serialize the intermediate resultIsinvarianttonulls =true,//Optimizer PropertyIsinvarianttoduplicates =false,//Optimizer PropertyIsinvarianttoorder =false,//Optimizer PropertyMaxByteSize =8000)//maximum size in bytes of persisted value] Public classmax_o3_8hour_nd:ibinaryserialize{/// <summary>    ///The variable that holds the intermediate result of the concatenation/// </summary>    PrivateStringBuilder Intermediateresult; /// <summary>    ///initialization of the system/// </summary>     Public voidInit () { This. Intermediateresult =NewStringBuilder (); }    /// <summary>    ///accumulate text content, except for null, usually separated by punctuation marks. /// </summary>    /// <param name= "value" ></param>     Public voidaccumulate (SqlString value) {if(value.) IsNull) {return; }         This. Intermediateresult.append (value. Value). Append (','); }    /// <summary>    ///Merge the partially computed aggregate with this aggregate. /// </summary>    /// <param name= "Group" ></param>     Public voidMerge (max_o3_8hour_nd Group) { This. Intermediateresult.append (Group.intermediateresult); }    /// <summary>    ///at the end of the call, the result of the aggregate function is returned/// </summary>    /// <returns></returns>     PublicSqlString Terminate () {stringOutput =string.        Empty; ArrayList List=NewArrayList (); if( This. intermediateresult! =NULL&& This. intermediateresult.length >0) {Output= This. intermediateresult.tostring (0, This. Intermediateresult.length-1); string[] result=output. Split (','); floatMax =0; if(result.) Length >=8)            {                 for(inti =0; I <= result. Length-8; i++)                {                    floatRe =0;  for(intj = 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 NewSqlString (output); }     Public voidRead (BinaryReader r) {Intermediateresult=NewStringBuilder (r.readstring ()); }     Public voidWrite (BinaryWriter w) {W.write ( This. intermediateresult.tostring ()); }}

DLL to SQL Server to create an aggregate function.

' C:\MAX_O3_8HOUR_ND.dll '  = 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 turns on CLR support:

' CLR enabled ' 1 RECONFIGURE with Overridego

Example:

SELECT fdate,sitename, dbo. Max_o3_8hour_nd (O3) from Monitor_nd
GROUP by Fdate,sitename
ORDER by Fdate,sitename

SQL Server 2012 custom aggregate function (MAX_O3_8HOUR_ND) calculates the maximum 8-hour sliding average of ozone

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.