PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Menghitung jumlah harian Kumulatif di PostgreSQL

Anda dapat menggunakan fungsi agregasi dan jendela bersama-sama:

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;

Anda dapat membagi jika Anda ingin persentase:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. kelompokkan berdasarkan kisaran harga

  2. Penyesuaian PostgreSQL:Hal Utama untuk Mendorong Kinerja

  3. PostgreSQL melalui SSH Tunnel

  4. Mengapa transaksi PostgreSQL ini memberikan PERINGATAN:tidak ada transaksi yang sedang berlangsung

  5. Skrip uji untuk konkurensi transaksi untuk postgresql