手机
当前位置:查字典教程网 >编程开发 >mssql数据库 >SQLServer2005 中的几个统计技巧
SQLServer2005 中的几个统计技巧
摘要:在SQLServer中我们可以用over子句中来代替子查询实现来提高效率,over子句除了排名函数之外也可以和聚合函数配合。实现代码如下:复...

SQLServer2005 中的几个统计技巧1

在SQLServer中我们可以用over子句中来代替子查询实现来提高效率,over子句除了排名函数之外也可以和聚合函数配合。实现代码如下:

复制代码 代码如下:

use tempdb

go

if (object_id ('tb' ) is not null )

drop table tb

go

create table tb (name varchar (10 ), val int )

go

insert into tb

select 'aa' , 10

union all select 'aa' , 20

union all select 'aa' , 20

union all select 'aa' , 30

union all select 'bb' , 55

union all select 'bb' , 45

union all select 'bb' , 0

select *

, 排名 = rank ()over (partition by name order by val )

, 占比 = cast (val * 1.0 / sum (val )over (partition by name ) as decimal (2 , 2 ))

, 距最大 = val - max (val )over (partition by name )

, 距最小 = val - min (val )over (partition by name )

, 距平均 = val - avg (val )over (partition by name )

from tb

【SQLServer2005 中的几个统计技巧】相关文章:

SQLServer2005 评估和管理索引

SQL Server各种日期计算方法

SQL SERVER2000 的一些技巧

SQL Server的ldf和mdf文件转移

SQL Server报错汇总

SQLServer中的通配符和转义字符

SQLServer中批处理的知识点

Sql Server中REPLACE函数的使用

SQL Server 2000中修改数据库COLLATE的实例

SQL Server 2005 返回修改后的数据

精品推荐
分类导航