sql >> Database >  >> RDS >> PostgreSQL

Cumulatieve dagelijkse som berekenen in PostgreSQL

U kunt aggregatie- en vensterfuncties samen gebruiken:

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;

U kunt delen als u het percentage wilt:

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. Hoe maak je een geneste tabel als database-object in Oracle?

  2. Hoe de identiteitskolom in SQL Server bij te werken?

  3. Selecteer kolomwaarde indien niet null anders gebruik een andere kolomwaarde

  4. MySQL Spatial CONTAINS geeft een verkeerd resultaat weer