PostgreSQL
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> PostgreSQL

Tính tổng tích lũy hàng ngày trong PostgreSQL

Bạn có thể sử dụng các chức năng tổng hợp và cửa sổ cùng nhau:

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;

Bạn có thể chia nếu bạn muốn tỷ lệ phần trăm:

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. Bộ đệm (Vòng tròn) trong PostGIS

  2. Tại sao việc giảm một chỉ mục lại mất nhiều thời gian hơn so với việc tạo ra nó?

  3. PGTune Alternatives - ClusterControl PostgreSQL Configuration

  4. Quyền kết xuất postgresql bị từ chối

  5. Nhóm múi giờ RoR Postgresql không hoạt động trên Heroku