zoukankan      html  css  js  c++  java
  • C#实现SQL SERVER2008聚合函数

    下面实现一个去掉最大值和最小值之后的平均值函数:

    单击项目右键,在弹出菜单中选择"添加"->"聚合",取名为TrimmedMean.cs,代码如下:

     1 using System;
     2 using System.Data;
     3 using System.Data.SqlClient;
     4 using System.Data.SqlTypes;
     5 using Microsoft.SqlServer.Server;
     6 
     7 
     8 [Serializable]
     9 [Microsoft.SqlServer.Server.SqlUserDefinedAggregate(Format.Native)]
    10 public struct TrimmedMean
    11 {
    12     //    SELECT   AVG(TotalDue) AS AvgTotal, 
    13     //dbo.TrimmedMean(TotalDue) AS TrimmedTotal
    14     //FROM      Sales.SalesOrderHeader
    15     public void Init()
    16     {
    17         // 在此处放置代码
    18         this.numValues = 0;
    19         this.totalValue = 0;
    20         this.maxValue = SqlMoney.MinValue;
    21         this.minValue = SqlMoney.MaxValue;
    22     }
    23 
    24     public void Accumulate(SqlMoney Value)
    25     {
    26         // 在此处放置代码
    27         if (!Value.IsNull)
    28         {
    29             this.numValues++;
    30             this.totalValue += Value;
    31             if (Value < this.minValue)
    32                 this.minValue = Value;
    33             if (Value > this.maxValue)
    34                 this.maxValue = Value;
    35         }
    36     }
    37 
    38     public void Merge(TrimmedMean Group)
    39     {
    40         // 在此处放置代码
    41         if (Group.numValues > 0)
    42         {
    43             this.numValues += Group.numValues;
    44             this.totalValue += Group.totalValue;
    45             if (Group.minValue < this.minValue)
    46                 this.minValue = Group.minValue;
    47             if (Group.maxValue > this.maxValue)
    48                 this.maxValue = Group.maxValue;
    49         }
    50     }
    51 
    52     public SqlMoney Terminate()
    53     {
    54         // 在此处放置代码
    55         if (this.numValues < 3)
    56             return (SqlMoney.Null);
    57         else
    58         {
    59             this.numValues -= 2;
    60             this.totalValue -= this.minValue;
    61             this.totalValue -= this.maxValue;
    62             return (this.totalValue / this.numValues);
    63         }
    64     }
    65 
    66     // 这是占位符成员字段
    67     private int numValues;
    68     private SqlMoney totalValue;
    69     private SqlMoney minValue;
    70     private SqlMoney maxValue;
    71 }


    单击项目右键,弹出菜单中先"生成",再"部署",在SQL SERVER 2008中测试如下:

    SELECT

      AVG(TotalDue) As AverageTotal,

      dbo.TrimmedMean(TotalDue) AS TrimedAverageTotal

    FROM Sales.SalesOrderHeader

    使用的是AdventureWorks示例数据库

  • 相关阅读:
    java编译错误No enclosing instance of type TestFrame is accessible. Must qualify the allocation with an enclosing instance of type TestFrame (e.g. x.new A(
    java 2中创建线程方法
    动态规划基本思想
    关于eclipse编译一个工程多个main函数
    java Gui初识
    Eclipse中java项目的打包
    java 播放声音
    把资源文件夹导入到eclipse中
    Java建立JProgressBar
    How to grant permissions to a custom assembly that is referenced in a report in Reporting Services
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2806123.html
Copyright © 2011-2022 走看看