0

This is my code:

select 
    referido,
    count(*) filter (where extract(month from fec_alta) = 3) ::float  as Marzo
from usuarios
where fec_alta between '2020-03-01' and '2020-04-30'
group by referido;

Now I need to divide the 2nd line by 76 (that its the sum of the column)

So I have this output

         march  
true     60    
false    16    

and I need this output

         march  
true     78,9 % 
false    21,1 % 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

Just use window functions:

select referido,
       (count(*) filter (where extract(month from fec_alta) = 3)::float  /
        sum(count(*) filter (where extract(month from fec_alta) = 3)) over ()
       ) as Marzo
from usuarios
where fec_alta between '2020-03-01' and '2020-04-30'
group by referido;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786