sql >> Base de Datos >  >> RDS >> PostgreSQL

Cuenta acumulada total con PostgresQL

Solo la respuesta publicada para cerrar la pregunta:

-- Set "1" for counting to be used later
WITH DATA AS (

SELECT

   orders.id, 
   orders.client_id, 
   orders.deliver_on,
   COUNT(1) -- Creates a column of "1" for counting the occurrences

   FROM orders

   GROUP BY 1

   ORDER BY deliver_on, client_id

)

SELECT

   id,
   client_id,
   deliver_on,
   SUM(COUNT) OVER (PARTITION BY client_id 
                           ORDER BY client_id, deliver_on 
                           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) -- Counts the sequential client_ids based on the number of times they appear

 FROM DATA