0

到目前为止,我一直在努力处理这个查询,我可以创建 1 分钟的间隔,但是我需要的两列的总和只给出了那个分钟,而不是随着时间的推移而累积的总和

SELECT
    TIMESTAMP WITH TIME ZONE 'epoch' +
    INTERVAL '1 second' * round(extract('epoch' from "Timestamp") / 60) * 60 as Timestamp",
    SUM("Particles"), sum("Bio")
from "Results" 
where "SampleID" = 50 
GROUP BY round(extract('epoch' from "Timestamp") / 60)
ORDER BY "Timestamp" DESC

那是我正在使用的查询,结果是这些

          Timestamp      |Sum |Sum
"2013-08-09 14:17:00-07" | 61 | 4
"2013-08-09 14:16:00-07" | 64 | 6
"2013-08-09 14:15:00-07" | 29 | 5
"2013-08-09 14:14:00-07" | 96 | 1
"2013-08-09 14:13:00-07" | 43 | 2

但我需要最后两列的累计

Select
    TIMESTAMP WITH TIME ZONE 'epoch' +
    INTERVAL '1 second' * round(extract('epoch' from "Timestamp") / 60) * 60 as "Timestamp",
    sum("Particles") over (order by "Timestamp") as Cumulative_Part, 
    sum("Bio") over (order by "Timestamp") as Cumulative_Bio from "Results" 
where
    "SampleID" = 50 and
    "Timestamp" between '2013-08-09 14:13:00' and '2013-08-09 14:17:00'
GROUP BY round(extract('epoch' from "Timestamp") / 60)
Order by "Timestamp" DESC
4

1 回答 1

0

如果您有 postgresql 8.4 或更高版本,则可以使用OVER(ORDER BY..)构造

SELECT...
, SUM("Particles") OVER(ORDER BY TimeStamp_Field) as SUM_Particles
...

看到这里了解更多信息

于 2013-08-20T21:11:49.997 回答