Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Postgres查询将15分钟间隔转换为每小时间隔_Sql_Postgresql_Date - Fatal编程技术网

Sql Postgres查询将15分钟间隔转换为每小时间隔

Sql Postgres查询将15分钟间隔转换为每小时间隔,sql,postgresql,date,Sql,Postgresql,Date,我想将postgres数据库中的15分钟间隔表转换为每小时间隔,并根据该表汇总其他列中的所有值。我该怎么做?问题是什么 例如: timestamp count "2015-01-05 12:00:00" 35 "2015-01-05 12:15:00" 45 "2015-01-05 12:30:00" 23 "2015-01-05 12:45:00" 23 "2015-01-05 01:00:00"

我想将postgres数据库中的15分钟间隔表转换为每小时间隔,并根据该表汇总其他列中的所有值。我该怎么做?问题是什么

例如:

timestamp                    count
"2015-01-05 12:00:00"          35
"2015-01-05 12:15:00"       45
"2015-01-05 12:30:00"       23
"2015-01-05 12:45:00"       23
"2015-01-05 01:00:00"       45
"2015-01-05 01:15:00"       12
"2015-01-05 01:30:00"       11
"2015-01-05 01:45:00"        56
我希望输出表是

timestamp                  count
2015-01-05 12:00:00         126
2015-01-05 01:00:00         124
简单,在PostgreSQL中:

SELECT date_trunc('hour', timestamp_col) as ts_hour, sum(count_col) 
FROM counts_table
GROUP BY ts_hour
ORDER BY ts_hour;
简单,在PostgreSQL中:

SELECT date_trunc('hour', timestamp_col) as ts_hour, sum(count_col) 
FROM counts_table
GROUP BY ts_hour
ORDER BY ts_hour;

FuzzChef给出了一个很好的答案,但您可能需要将订单从别名更改为实际日期,如下所示:

SELECT date_trunc('hour', timestamp_col) as ts_hour, sum(count_col) FROM counts_table 
GROUP BY date_trunc('hour', timestamp_col) 
ORDER BY date_trunc('hour', timestamp_col) 
;
此外,如果您只想显示小时,请使用extract,如中所示:


FuzzChef给出了一个很好的答案,但您可能需要将订单从别名更改为实际日期,如下所示:

SELECT date_trunc('hour', timestamp_col) as ts_hour, sum(count_col) FROM counts_table 
GROUP BY date_trunc('hour', timestamp_col) 
ORDER BY date_trunc('hour', timestamp_col) 
;
此外,如果您只想显示小时,请使用extract,如中所示:


GROUP BY中允许引用输出列名,ORDER BY和acutally优先于输入列名。FuzzyChef的答案在这方面更好。您的第二个查询将把多天的时间集中在一起,这很可能是无意的。非常感谢。这真的很有帮助。如果有一个id定义了它们中的每一个,那么我只需在选择字段中添加id,对吗?当然可以,然后您需要在该字段上执行group by操作,即将其添加到现有group by字段中group by和ORDER by中允许对输出列名进行引用,并且实际优先于输入列名。FuzzyChef的答案在这方面更好。您的第二个查询将把多天的时间集中在一起,这很可能是无意的。非常感谢。这真的很有帮助。如果有一个id定义了它们中的每一个,那么我也只需在选择字段中添加id,对吗?当然可以,然后您需要在该字段上执行group by,即将其添加到现有group by字段中