我有一个带有时间戳字段和用法字段的表,如下所示
timestamp | usage
'2015-06-13 13:45:58' | 240
'2015-06-13 13:45:59' | 480
'2015-06-13 13:46:00' | 240
'2015-06-13 13:46:01' | 320
...
我希望获得一段时间“1周”和“30分钟”间隔的使用总和。 我只能以分钟,小时,天和......的间隔获取数据。
SELECT date_trunc('minute', timestamp) as clock, sum(usage)
FROM my_table
WHERE timestamp > localtimestamp - INTERVAL '1 week'
GROUP BY clock
如何获取“5分钟”,“30分钟”,“2天”和......等间隔的数据。
答案 0 :(得分:2)
使用以下内容获取您所追求的内容。
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + INTERVAL '1 second' * round((extract('epoch' FROM timestamp) / 1800) * 1800) AS clock, SUM(usage)
FROM my_table
WHERE timestamp > localtimestamp - INTERVAL '1 week'
GROUP BY round(extract('epoch' FROM timestamp) / 1800)