PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Contagem total em execução com PostgresQL


Apenas a resposta postada para fechar a pergunta:
-- 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