mssql sqlserver 使用sql脚本计算数据列的百分比的方法分享


摘要:
下文讲述sqlserver中使用脚本计算数据列占”此列总数据”的百分比的方法分享,如下所示:
实验环境:sql server 2008 R2


例:
求下表中“列A”占“列A和”的百分比,具体方法如下:

  create table [maomao365.com]
(keyId int identity,
 a    int)
 go
 insert into [maomao365.com](a)
 values(5),(6),(7),(8),
 (10),(2),(3),(15),(21),(3)
 go

 select keyId,a,left(convert(varchar(100),
               (
			     a/convert(decimal(18,4),a_All)
			    )*100)
			    ,4)+'%' from 
 (
 select keyId,a,sum(a) over() as a_All from [maomao365.com]
 ) as t 

 go
 truncate table [maomao365.com]
 drop     table [maomao365.com]
mssql_sqlserver_列占比计算方法_9589

mssql_sqlserver_列占比计算方法_9589