在SQLServer中我們可以用over子句中來代替子查詢實(shí)現(xiàn)來提高效率,over子句除了排名函數(shù)之外也可以和聚合函數(shù)配合。實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
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
您可能感興趣的文章:- SQLSERVER收集語句運(yùn)行的統(tǒng)計(jì)信息并進(jìn)行分析
- SQL Server自動(dòng)更新統(tǒng)計(jì)信息的基本算法
- sqlserver 統(tǒng)計(jì)sql語句大全收藏
- SQLSERVER語句的執(zhí)行時(shí)間顯示的統(tǒng)計(jì)結(jié)果是什么意思
- 淺談SQL Server中統(tǒng)計(jì)對(duì)于查詢的影響分析
- SQL Server統(tǒng)計(jì)信息更新時(shí)采樣百分比對(duì)數(shù)據(jù)預(yù)估準(zhǔn)確性的影響詳解