简体   繁体   English

如何为时间戳添加分钟?

[英]How to add minutes to a timestamp?

I have a table which has data in 5 minute intervals ie 5 10 15 20 ... 我有一个表,其中每隔5分钟就有数据,即5 10 15 20 ...

The table has 3 columns (id number, log_ts date , count number) and has millions of rows. 该表有3列(ID号,log_ts日期,计数号),并具有数百万行。 So far I have been using the below query to sum up the "count" column for a particular date . 到目前为止,我一直在使用以下查询来总结特定日期的“计数”列。

I need to aggregate on an hourly level 我需要每小时汇总

select id, SUM(count)
     log_ts + DECODE(TO_CHAR(log_ts , 'MI'), '05', 55/1440, '10', 50/1440, '15', 45/1440, '20',40/1440,'25',35/1440,'30',30/1440,'35',25/1440,'40',20/1440,'45',15/1440,'50',10/1440,'55',5/1440,0) TS
from example_table 
Group by id,
log_ts + DECODE(TO_CHAR(log_ts , 'MI'), '05', 55/1440, '10', 50/1440, '15', 45/1440, '20',40/1440,'25',35/1440,'30',30/1440,'35',25/1440,'40',20/1440,'45',15/1440,'50',10/1440,'55',5/1440,0)

Is there any other way to aggregate a time column at an hourly level which has data in 5 minutes level. 还有其他方法可以将每小时列的时间列汇总到5分钟内的数据。

You need to use TRUNC() , which can truncate a date/number etc to the unit of time specified. 您需要使用TRUNC() ,它可以将日期/数字等截断为指定的时间单位。

For hourly, the format model is HH . 对于每小时, 格式模型HH This makes your query: 这使您的查询:

select trunc(log_ts, 'HH'), sum(count)
  from example_table
 group by trunc(log_ts, 'HH')

I assume the name of your column isn't actually COUNT . 我认为您的列名实际上不是COUNT If it is I would recommend changing this to something that isn't a keyword. 如果是这样,我建议将其更改为不是关键字的内容。

Try like this, 这样尝试

SELECT id, trunc(log_ts, 'HH'), sum(count)
FROM   example_table
GROUP BY id, trunc(log_ts, 'HH');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM