【发布时间】:2020-07-15 15:00:41
【问题描述】:
我想按 avfamily 分组,选择 livingofftheland 值等于 true 的记录数量,并将其作为 perc 值返回。
基本上是第 3 列除以第 2 列乘以 100。
select
avclassfamily,
count(distinct(malware_id)) as cc,
sum(case when livingofftheland = 'true' then 1 else 0 end),
(100.0 * (sum(case when livingofftheland = 'true' then 1 else 0 end) / (count(*)) ) ) as perc
from malwarehashesandstrings
group by avclassfamily having count(*) > 5000
order by perc desc;
可能很简单,但我的大脑在这里一片空白。
【问题讨论】:
-
那么问题出在哪里?
-
distinct不是一个函数,它是一个集合量词。跳过那些多余的括号,直接写count(distinct malware_id) as cc让代码更清晰。
标签: sql postgresql group-by percentage