如何计算SQL中计数的百分比

时间:2018-08-02 21:16:38

标签: sql

enter image description here

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

eggplant<-function(x){
(if((x == (dflist4[["Book1"]])){
maxm = 3;
x %>% mutate(Col4 = (x[,3])/maxm);
})
(if((x == dflist4[["Book2"]])){
maxm = 2;
x %>% mutate(Col4 = (x[,3])/maxm);
})
(if((x == dflist4[["Book3"]])){
maxm = 1;
x %>% mutate(Col4 = (x[,3])/maxm);
})
}

test<-lapply(dflist4, eggplant)

3 个答案:

答案 0 :(得分:0)

类似该查询的内容将为您服务。

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;

我尚未执行此操作,但这是您如何解决给定问题的想法。

答案 1 :(得分:0)

选择follower_account_id,(COUNT(distinct(followee_account_id))/ @ totalfollowers)* 100作为关注者  来自pw_social_relation  GROUP BY follower_account_id  HAVING Count(distinct(followee_account_id))> = 3

答案 2 :(得分:0)

首先计算关注者,然后计算其中至少三个关注者。

无论如何,乍一看这似乎是正确的...

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;

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