sql >> Databáze >  >> RDS >> PostgreSQL

Výpočet kumulativního denního součtu v PostgreSQL

Agregační a okenní funkce můžete používat společně:

select date_trunc('day', date_created) as day_created,
       location_state,
       count(*) opp_count,
       sum(count(*)) over (partition by location_state order by min(date_created)) as cumulative_opps_received
from t
group by day_created
order by location_state, day_created;

Pokud chcete procenta, můžete je rozdělit:

select date_trunc('day', date_created) as day_created,
       location_state,
       count(*) opp_count,
       sum(count(*)) over (partition by location_state order by min(date_created))  as cumulative_opps_received,
       (sum(count(*)) over (partition by location_state order by min(date_created)) * 1.0 /
        sum(count(*)) over (partition by location_state)
       ) as cumulative_ratio
from t
group by day_created
order by location_state, day_created;



  1. C# a PostgreSQL

  2. Sestavte vkládací dotaz z pole MySQL a PHP

  3. Python:MySQL:Zpracování časových limitů

  4. MariaDB JSON_ARRAY_INSERT() Vysvětleno