首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在sql中计算计数百分比

如何在sql中计算计数百分比
EN

Stack Overflow用户
提问于 2018-08-03 05:16:38
回答 3查看 66关注 0票数 -3

有人知道如何解决这个问题吗?这是我能做的最好的了,但我不认为它是正确的。

代码语言:javascript
复制
With
(select Count(distinct(follower_account_id) as followers
 From pw_social_relation)
Select count(distinct(follower_account_id))/followers
From pw_social_relation
Where count(followee_account_id)>=3
EN

回答 3

Stack Overflow用户

发布于 2018-08-03 05:29:07

类似这样的查询将为您工作。

代码语言:javascript
复制
select ((b.result/a.total)*100) as percentage from 
(select count(distinct(follower_account_id)) as total from pw_social_relation) a
,(select count(follower_account_id) as result from pw_social_relation group by follower_account_id having count(*) >= 3) b;

我没有执行这个,但这是一个想法,如果你能解决给定的问题。

票数 0
EN

Stack Overflow用户

发布于 2018-08-03 17:37:19

SELECT follower_account_id,(COUNT(distinct(followee_account_id))/@totalfollowers) * 100 as followersPer From pw_social_relation GROUP BY follower_account_id HAVING Count(distinct(followee_account_id)) >= 3

票数 0
EN

Stack Overflow用户

发布于 2018-08-03 21:06:39

首先计算关注量,然后计算至少三个关注量的账户。

总之,虽然乍一看这看起来是正确的.

代码语言:javascript
复制
select
  count(case when following >= 3 then 1 end) / count(*) as percentage
from
(
  select follower_account_id, count(*) as following
  from pw_social_relation
  group by follower_account_id
) counted;

..。我认为我们可以推测,可能有一些账户没有关注任何人。因此,我们必须与某些帐户表中的帐户数进行比较。但从这里开始,您可以很容易地相应地调整查询:-)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51662277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档