C#写的SQL聚合函数
SQL Server 字符串连接聚合函数.
- 注册程序集: 拷贝“SqlStrConcate.dll”至<sql安装根目录>/MSSQL.1/MSSQL/Binn目录下,执行下面的SQL:
CREATE ASSEMBLY [SqlStrConcate]
AUTHORIZATION [dbo]
FROM 'D:/Program Files/Microsoft SQL Server/MSSQL.1/MSSQL/Binn/SqlStrConcate.dll'
WITH PERMISSION_SET = SAFE
GO上面的代码中, <sql安装根目录>为D:/Program Files/Microsoft SQL Server/
- 创建自定义函数:
CREATE AGGREGATE StrConcate (@input nvarchar(200)) RETURNS nvarchar(max)
EXTERNAL NAME SqlStrConcate.Concatenate - 打开SQL Server 的CLR集成.方法: SQL Server 外围应用配置器 -> 功能的外围应用配置器 -> MSSQLSERVER -> Database Engine -> CLR集成 。勾选启用CLR集成。
- OK了,现在你可以使用用户自定义字符串聚合函数StrConcate.测试代码如下:
--------------------------------------------------------------
-- 创建测试表
--------------------------------------------------------------
create table test_tb(pk_val int, startdate varchar(10), enddate VARCHAR(10), corpname VARCHAR(20)) --------------------------------------------------------------
-- 插入测试数据
--------------------------------------------------------------
insert into test_tb
select 1, '2005-01-01', '2007-06-29', '方正科技'
union all
select 1, '2007-07-01', '2009-06-29', '清华紫光'
union all
select 1, '2009-01-01', null, '用友软件'
union all
select 2, '1995-01-01', '2003-06-29', '微软中国'
union all
select 2, '2004-07-01', '2009-06-29', '盛大网络'
go --------------------------------------------------------------
-- 查询测试
--------------------------------------------------------------
select pk_val, dbo.StrConcate(startdate + '~' + isnull(enddate, '至今') + ':' + corpname) lvl_str from test_tb group by pk_val --------------------------------------------------------------
-- 查询结果
--------------------------------------------------------------
pk_val lvl_str
----------- -----------------------------------------------------------------------------------------
1 2005-01-01~2007-06-29:方正科技,2007-07-01~2009-06-29:清华紫光,2009-01-01~至今:用友软件
2 1995-01-01~2003-06-29:微软中国,2004-07-01~2009-06-29:盛大网络- SqlStrConcate.dll 代码(来自Sql Server的联机帮助)如下:
- using System; using System.Data; using Microsoft.SqlServer.Server; using System.Data.SqlTypes; using System.IO; using System.Text;
[Serializable] [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 Concatenate : IBinarySerialize { /// <summary> /// The variable that holds the intermediate result of the concatenation /// </summary> private StringBuilder intermediateResult;
/// <summary> /// Initialize the internal data structures /// </summary> public void Init() { this.intermediateResult = new StringBuilder(); }
/// <summary> /// Accumulate the next value, not if the value is null /// </summary> /// <param name="value"></param> public void Accumulate(SqlString value) { if (value.IsNull) { return; }
this.intermediateResult.Append(value.Value).Append(','); }
/// <summary> /// Merge the partially computed aggregate with this aggregate. /// </summary> /// <param name="other"></param> public void Merge(Concatenate other) { this.intermediateResult.Append(other.intermediateResult); }
/// <summary> /// Called at the end of aggregation, to return the results of the aggregation. /// </summary> /// <returns></returns> public SqlString Terminate() { string output = string.Empty; //delete the trailing comma, if any if (this.intermediateResult != null && this.intermediateResult.Length > 0) { output = this.intermediateResult.ToString(0, this.intermediateResult.Length - 1); }
return new SqlString(output); }
public void Read(BinaryReader r) { intermediateResult = new StringBuilder(r.ReadString()); }
public void Write(BinaryWriter w) { w.Write(this.intermediateResult.ToString()); } }
C#写的SQL聚合函数的更多相关文章
- SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。
top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 orde ...
- SQL 聚合函数
SQL聚合函数 MAX---最大值 MIN--最小值 AVG--平均值 SUM--求和 COUNT--记录的条数 EXample: --从MyStudent表中查询最大年龄,最小年龄,平均年龄,年龄的 ...
- Sql Server的艺术(三) SQL聚合函数的应用
SQL提供的聚合函数有求和,最大值,最小值,平均值,计数函数等. 聚合函数及其功能: 函数名称 函数功能 SUM() 返回选取结果集中所有值的总和 MAX() 返回选取结果集中所有值的最大值 MIN( ...
- sql 聚合函数、排序方法详解
聚合函数 count,max,min,avg,sum... select count (*) from T_Employee select Max(FSalary) from T_Employee 排 ...
- Spark学习之路(十一)—— Spark SQL 聚合函数 Aggregations
一.简单聚合 1.1 数据准备 // 需要导入spark sql内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSess ...
- Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations
一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...
- sql 聚合函数用法,及执行顺序
聚合函数无法用在where子句中 , 聚合函数包括count avg sum min max 子句执行顺序from -> where -> group by -> having -& ...
- sql 聚合函数和group by 联合使用
原文 很多时候单独使用聚合函数的时候觉得很容易,求个平均值,求和,求个数等,但是和分组一起用就有点混淆了,好记性不如烂笔头,所以就记下来以后看看. 常用聚合函数罗列 1 AVG() - 返回平均值 C ...
- SQL聚合函数
随机推荐
- [改善Java代码]减少HashMap中元素的数量
在系统开发中我们经常会使用HashMap作为数据集容器,或者是用缓冲池来处理,一般很稳定,但偶尔也会出现内存溢出的问题(OutOfMemory错误),而且这经常是与HashMap有关的.而且这经常是与 ...
- MSP430常见问题之指令系统类
Q1. IAR中怎样描述P2OUT.3脚,#define LCD_cs1 P2OUT.3; 对吗?A1:430 不能位寻址,所以一般的位操作,都通过“与”来作用.#define LCD_cs1 (P2 ...
- 练习题之Wait/Notify
方案一: public class PrintABC { public static void main(String[] args) { AtomicInteger synObj = ); Runn ...
- Angular2 从0到1 (一)
史上最简单Angular2教程,大叔都学会了 作者:王芃 wpcfan@gmail.com 第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:An ...
- 详解Win2003 IIS6.0 301重定向带参数的问题(转摘)
网站更换域名,把旧域名用301指到新域名来. 从iis中设置url永久转向就可以,看上去很容易,用了一会儿才发现,参数都没有带上. 从微软网站上找到如下说明,果然好使: 重定向参考 (IIS 6. ...
- ifndef/define/endif作用和用法
问题:ifndef/define/endif”主要目的是防止头文件的重复包含和编译,偶只知道这个概念不懂的是怎么个用法,和为什么要用它~~高手请指点一下~~谢谢~~~!!! ------------- ...
- 了解下SoftReference
昨天同事看到别人一段关于实现缓存功能的代码,看完之后他有点不明觉厉,哈哈,然后就给周围同事也看了下,可能之前大家都没用过SoftReference,所以并不明白是如何实现的. 于是我就把代码要了过来, ...
- 资源汇集:nginx教程从入门到精通
http://linux.cn/article-4279-1.html
- ASP.NET实现折线图的绘制
用到.Net中绘图类,实现折线图的绘制,生成图片,在页面的显示,代码如下: /// <summary> /// 获取数据 /// strChartName:图名称: /// yName:纵 ...
- OC5_NSFileManger
// // main.m // OC5_NSFileManger // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zh ...