Postgres连续累积计数

时间:2017-11-13 07:21:49

标签: postgresql sum cumulative-sum

假设表a包含数据

| id | timestamp   |  
|----|-------------|
| 1  | 12345677677 |  
| 2  | 12345677677 |  
| 3  | 12346600000 | 

我需要一个查询,它返回给定时间点的累积计数

| timestamp   | count| 
|-------------|------|
| 12345677677 | 2    | 
| 12346600000 | 3    |

所以不是按时间戳计数()组,而是按时间戳+先前计数计数()组。

感谢@klin这完美无缺;但是使用窗口函数,我在每天获得非常计数(*)和一个查询中的累积总和时遇到问题。

SELECT date_trunc('day', date_created) AS date, 
       sum, 
       count(*) 
FROM   addon_user, 
       (SELECT DISTINCT ON(date_trunc('day', date_created)) 
       date_trunc('day', date_created), 
       count(*) 
       OVER ( 
         ORDER BY date_trunc('day', date_created)) AS sum 
        FROM   addon_user
) AS sub 

GROUP  BY date, 
          sum 
ORDER  BY date ASC

返回笛卡尔积:

timestamp  | count |sum
-------------+-------+---
12345677677 |     2|2
12345677677 |     2|5
12346600000 |     3|2
12346600000 |     3|5

虽然我需要

形式的数据
timestamp  | count |sum
-------------+-------+---
12345677677 |     2|2
12346600000 |     3|5

1 个答案:

答案 0 :(得分:3)

使用count()作为具有order by子句的窗口函数。 Postgres中有一个很好的功能,distinct on可以消除重复的行:

select distinct on(timestamp) timestamp, count(*) over (order by timestamp)
from my_table;

  timestamp  | count 
-------------+-------
 12345677677 |     2
 12346600000 |     3
(2 rows)