Select postgresql 中另一列的百分比

Select percentage of another column in postgresql

我想 select,按 avfamily 分组,将 livingofftheland 值等于 true 的记录数量 return 作为 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;

可能很简单,但我的大脑在这里一片空白。

select, grouped by avfamily, the amount of records that have livingofftheland value equalling true and return it as the perc value.

为此您可以简单地使用 avg()

select 
    avclassfamily, 
    count(distinct(malware_id)) as cc, 
    avg(livingofftheland::int) * 100 as perc 
from malwarehashesandstrings 
group by avclassfamily
having count(*) > 5000
order by perc desc

livingofftheland::int 将布尔值变为 0(假)或 1(真)。该值的平均值为您提供组中满足条件的记录的比率,作为 01 之间的小数,然后您可以乘以 100

我会表达为:

select avclassfamily, 
       count(distinct malware_id) as cc, 
       count(*) filter (where livingofftheland = 'true'),
       ( count(*) filter (where livingofftheland = 'true') * 100.0 /
         count(distinct malware_id)
       ) as perc
from malwarehashesandstrings 
group by avclassfamily 
having count(*) > 5000  
order by perc desc;

请注意,这用 filter 替换了条件聚合,这是 Postgres 支持的 SQL 标准构造。它还将 100.0 放在 / 旁边,以确保 Postgres 不会决定进行整数除法。