PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

postgresql:ottieni il conteggio per intervalli di valori

select name, 
       count(case when value <= 5 then 1 end) as "0-5",
       count(case when value > 5 and value <= 10 then 1 end) as "5-10",
       count(case when value > 10 and value <= 15 then 1 end) as "10-15"
from the_table
group by name;

Con la prossima versione 9.4 questo può essere scritto un po' più leggibile:

select name, 
       count(*) filter (where amount <= 5) as "0-5",
       count(*) filter (where value > 5 and value <= 10) as "5-10",
       count(*) filter (where value > 10 and value <= 15) as "10-15"
from the_table
group by name;