日期:2014-05-18  浏览次数:20708 次

自定义聚合函数求中位数
vs.net->新建项目->C#->数据库项目->新建一个聚合函数

C# code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Collections.Generic;

[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined,
                         MaxByteSize=8000,
                         IsNullIfEmpty=true,
                         Name = "MiddleNumber")]
public class MiddleNumber:IBinarySerialize 
{
    public void Init()
    {
        list = new List<double>();
    }

    public void Accumulate(SqlDouble Value)
    {
        if (Value.IsNull == false)
            list.Add(Value.Value);
    }

    public void Merge(MiddleNumber Group)
    {
        list.AddRange(Group.list);
    }

    public SqlDouble Terminate()
    {
        double re = 0;
        int lCount = 0;

        list.Sort();

        lCount = list.Count;

        if (lCount > 0)
        {
            if (lCount % 2 == 0)
            {
                re = (double)((Convert.ToDouble(list[lCount / 2 - 1]) + Convert.ToDouble(list[lCount / 2])) / 2.0);
            }
            else
            {
                re = (double)(Convert.ToDouble(list[Convert.ToInt32(Math.Floor(lCount / 2.0))]));
            }

            return new SqlDouble(re);
        }
        else
        {
            return SqlDouble.Null;
        }
    }

    public void Write(BinaryWriter writer)
    {
        int lCount = list.Count;

        writer.Write(lCount);

        if (lCount > 0)
        {
            foreach (double number in list)
            {
                writer.Write(number);
            }
        }
    }

    public void Read(BinaryReader reader)
    {
        int lCount = reader.ReadInt32();
        list = new List<double>();

        for (int i = 0; i < lCount; i++)
        {
            list.Add(reader.ReadDouble());
        }
    }

    // 这是占位符成员字段
    private List<double> list;

}


sql server management studio 

SQL code
CREATE ASSEMBLY ass_test
FROM 'G:\CLR_Test\SqlServerProject5\SqlServerProject5\bin\Debug\SqlServerProject5.dll';
GO

CREATE AGGREGATE MiddleNumber(@number float)
    RETURNS float
EXTERNAL NAME ass_test.MiddleNumber;
GO


declare @t table(id int,val float)
insert @t select 1,20.2
union all select 1,50
union all select 1,20

union all select 2,50
union all select 2,20

union all select 3,null

union all select 4,0

--查询
select id,dbo.MiddleNumber(val) as 中位数
from @t
group by id

/*
id          中位数
----------- ----------------------
1           20.2
2           35
3           NULL
4           0

(4 行受影响)
*/


------解决方案--------------------
学习
------解决方案--------------------
牛X
------解决方案--------------------
用来学习的,顶。。。
------解决方案--------------------
public void Init()
{
list = new List<double>();
}
呜 ..梁哥这个list 泛型变量在哪声明的呀???
------解决方案--------------------
学习
------解决方案--------------------
探讨
引用:
public void Init()
    {
        list = new List <double>();
    }
呜 ..梁哥这个list 泛型变量在哪声明的呀???


这个类的最后面呀.

------解决方案--------------------