-1

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

我开始:

选择 ts_title

COUNT (ts_title) AS 百分比

帮我继续,谢谢

4

5 回答 5

1

这可能会给你你正在寻找的东西

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

乘以 100.0 将其转换为浮点数以获得更准确的百分比。

于 2012-04-13T03:50:17.303 回答
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
于 2012-04-13T18:19:14.173 回答
0

尝试这个

SELECT ts_title,
(count(ts_title) * 100 / (SELECT count(*) FROM URP_PROJECT)) as percentage 
FROM URP_PROJECT Group BY ts_title
于 2012-04-13T03:53:20.840 回答
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;
于 2012-04-13T03:54:14.617 回答
0

像这样的东西,也许:

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

有用的阅读:

于 2012-04-13T18:27:31.973 回答