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

Beregning af kumulativ daglig sum i PostgreSQL

Du kan bruge aggregerings- og vinduesfunktioner sammen:

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;

Du kan dividere, hvis du vil have procenten:

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. Sådan bruges Bulk Collect-klausul med SELECT INTO-erklæring i Oracle-databasen

  2. Sådan gør du Drupal 7 redigerbart felt i tabellen kun for admin

  3. DatabaseError:værdien er for lang til at typetegn varierer (100)

  4. MySQL VÆLG LIKE eller REGEXP for at matche flere ord i én post