【问题标题】:Oracle SQL group row count by time intervals of 10 minutesOracle SQL 组行计数按 10 分钟的时间间隔
【发布时间】:2016-04-15 20:32:00
【问题描述】:

我在 oracle 中有一个表,其中包含如下数据

created_date 详细信息 2016 年 1 月 1 日 04:45 abcd 2016 年 1 月 1 日 04:47 2016 年 1 月 1 日 04:53 2016 年 1 月 1 日 04:54 01-Jan-16 04:58 qrst

....等

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

时间计数 04:40 2 04:50 3

创建日期 = 时间戳, 详细信息 = varchar

我该怎么做?

谢谢

【问题讨论】:

  • 您是否想要特定日期/时间范围的这些结果,其中 0 值的间隔没有任何匹配,或者您想要所有时间但只有至少有一个匹配的范围?
  • 另外,如果您有 01-Jan-16 04:45 和 02-Jan-16 04:46,您是否希望在单个 04:40 行中看到 2 的计数,或者您希望在 04:40 行中有单独的每天?

标签: sql oracle count group-by intervals


【解决方案1】:

如果您想根据实际时间戳值进行分组,这里有一个解决方案:

create table test_10_minutes_group_by (created_date timestamp, details varchar2(4));

insert into test_10_minutes_group_by values (systimestamp, 'aaa'); -- current time
insert into test_10_minutes_group_by values (systimestamp - 1/24/60, 'bbb'); -- 1 minute ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 10, 'ccc'); -- 10 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 20, 'ccc2'); -- 20 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 25, 'abc');  -- 25 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 30, 'xyz'); -- 30 minutes ago
insert into test_10_minutes_group_by values (systimestamp - 1/24/60 * 35, 'xyz2'); -- 35 minutes ago

select 
    actual_time,
    to_char(actual_time, 'hh24:mi:ss') pretty_date,
    count(1)
  from (
    select 
        trunc(created_date, 'mi') /*remove seconds*/ -  1/24/60 * mod(extract (minute from created_date), 10) /*substract units digit from minutes*/ actual_time, 
        details
      from 
        test_10_minutes_group_by 
  ) 
  group by actual_time;

【讨论】:

    【解决方案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】:

      您可以使用 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;
      

      【讨论】:

        【解决方案4】:

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

        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)
        

        【讨论】:

          猜你喜欢
          • 2022-01-22
          • 2016-09-21
          • 2020-04-10
          • 2014-01-26
          • 1970-01-01
          • 1970-01-01
          • 2010-09-30
          • 2015-02-01
          • 2019-04-10
          相关资源
          最近更新 更多