SQLServer2005 中的几个统计技巧


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',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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...