Oracle SQL组的行数按时间间隔为10分钟

时间:2016-01-11 13:47:08

标签: sql oracle count group-by intervals

我在oracle中有一个包含以下数据的表

created_date       details  
01-Jan-16 04:45    abcd  
01-Jan-16 04:47    efgh  
01-Jan-16 04:53    ijkl  
01-Jan-16 04:54    mnop  
01-Jan-16 04:58    qrst  

....等

我希望能够每10分钟计算一次表中的行数 例如

Time    count
04:40       2
04:50       3

创建日期=时间戳, details = varchar

我该怎么做?

由于

4 个答案:

答案 0 :(得分:4)

您可以使用TO_CHAR和SUBSTR来构建时间字符串:

select
  substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0' as created,
  count(*)
from mytable
group by substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0'
order by substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0';

或者使用子查询(派生表),以便只需要编写一次日期表达式:

select created, count(*)
from
(
  select substr(to_char(created_date, 'hh24:mi'), 1, 4) || '0' as created
  from mytable
)
group by created
order by created;

答案 1 :(得分:2)

一种方法是提取小时和分钟并进行算术运算:

select extract(hour from created_date) as hh,
       floor(extract(minute from created_date) / 6) as min,
       count(*)
from t
group by extract(hour from created_date),
         floor(extract(minute from created_date) / 6)

答案 2 :(得分:2)

答案是:

select trunc(sysdate, 'hh')+ trunc(to_char(sysdate,'mi')/10)*10/1440 from dual;

您可以将sysdate替换为您的实际日期/时间戳列,并将其替换为您的表格

要了解组件,请运行:

select trunc(sysdate, 'hh') the_hour,
   to_char(sysdate,'mi') the_minutes,
   trunc(to_char(sysdate,'mi')/10)*10 minutes_truncated,
   trunc(to_char(sysdate,'mi')/10)*10/1440 part_of_the_day, --as 1 represents a day in oracle datetime system
   trunc(sysdate, 'hh')+ trunc(to_char(sysdate,'mi')/10)*10/1440 result
from dual;

答案 3 :(得分:0)

如果您想在实际时间戳值上创建分组,这是一个解决方案:

mul