2012-04-13 139 views
-1

我有一张表,名为URP_PROJECT,列名为ts_title。列ts_title下有不同的记录数据。如何计算每个记录下ts_title的百分比? 百分比列是根据下面的SQL查询添加。SQL - 百分比计数

我开始:

SELECT ts_title

COUNT(ts_title)为百分比

帮助我继续吧,谢谢

+1

什么是你的MS SQL Server,MySQL和Oracle数据库? – 2012-04-13 03:42:02

+0

MS SQL Server 2008的 – Mohan 2012-04-13 03:44:42

+0

SSRS - SQL Server报表服务 – Mohan 2012-04-13 03:45:20

回答

1

这可能给你你在找什么

select ts_title, 
     count(1) * 100.0/(select count(1) from upr_project) 
from upr_project 
group by ts_title 

乘次100.0并将其转换为自动对焦嘲笑更准确的百分比。

+0

谢谢!转换为百分比帮助的浮点数增加到100. – kravits88 2014-01-03 04:19:59

0

试试这个

SELECT ts_title, 
(count(ts_title) * 100/(SELECT count(*) FROM URP_PROJECT)) as percentage 
FROM URP_PROJECT Group BY ts_title 
0

您好我猜你正在寻找ts_title记录在整个表的百分比。如果是这样的话,这将有助于你

DECLARE @ActualCount INT 

select @ActualCount = COUNT(*) from urp_project 


select ts_title 
    ,(Count(ts_title)/ @ActualCount)* 100 AS Percentage  
from urp_project 
group by ts_title; 
1

另一种解决方案是使用窗口功能:

select ts_title, 
     100.0 * (up.cnt*1.0/(sum(up.cnt) over (partition by 1))) 
from 
(
    select ts_title, count(*) as cnt 
    from upr_project 
    group by ts_title 
) up 
+0

此解决方案也适用于使用GROUPING SETS,ROLLUP或CUBE的“总计”行。您只需将up.cnt乘以2.0而不是1.0,即可解释“总计”行的额外音量。 – supergrady 2014-08-06 04:27:49

0

事情是这样的,也许:

SELECT 
    ts_title, 
    COUNT(*) * 100.0/SUM(COUNT(*)) OVER() AS percentage 
FROM URP_PROJECT 
GROUP BY ts_title 

有用的书: