sql >> Databasteknik >  >> RDS >> PostgreSQL

Beräknar kumulativ daglig summa i PostgreSQL

Du kan använda aggregerings- och fönsterfunktioner tillsammans:

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 dividera om du vill ha 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. Hur får man ett index av olika kategorier returnerat efter order av sql i Oracle?

  2. Hur man importerar oracle dump från ec2-instans till RDS i AWS

  3. Lägg till ett databaspostkonto till en profil (T-SQL)

  4. Hur ska jag rensa databasindata i Java?