SQL statistics by time period (for example, statistics of one visit in 5 minutes, oracle statistics)

Requirement: Count the number of visits on the day, and collect it every five minutes. There is a creation date in the

table structure, and the type is TIMESTAMP



. Scheme 1: Temporary table/temporary storage

Count the number of visits every 5 minutes and store it in a temporary table or temporary storage (such as excel)

loop The entire time period of the day

and then sort the temporary table/temporarily stored data



Option 2: SQL query of pseudo-columns (data volume is less than 100 million data volume, and the time column is indexed)

analysis: once every 5 minutes, then for each entry The record is a pseudo-column, and the data every 5 minutes is an identifier.

For example: creation time = 201604061200 - to - 201604061204, these five minutes are uniformly identified as 201604061200

   creation time = 201604061205 - to- 201604061209, these five minutes are uniformly identified as 201604061205, which

is very convenient to implement:

a. Take out the minute parameter of the creation time: to_char( t.create_time,'mi'), both are two digits: such as 01 points, 07 points, 13 points, 19 points....

    Only the second digit is judged. If the second digit is less than 5, the second digit of the minute will be uniformly assigned. = 0, otherwise (the second digit >= 5) uniformly let the second digit

    of the minute = 5 and the first digit of the minute remain unchanged. For

    example, 01 minutes and 04 minutes are uniformly marked as 00 points, and 05, 06, and 09 points are uniformly marked as 05 points

    to cut the string using the substr function

    to judge the use of case when

    splicing use ||

b. Take out the year, month, day, and hour parameters of the creation time, and assemble the special minute parameter of the first step.

  A pseudo-column was born like this

关键SQL如下:


case when substr(to_char(t.create_time,'mi'),2,1)<5 then to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||0
         else to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||5 end as newTime
上一个完整的sql


select count(tmp.id) totalNum,tmp.newTime
from(
  select t.id,t.latest_status, -- ID,状态
         to_char(t.create_time,'yyyymmddhh24mi') oldTime, -- 原来的时间
         case when substr(to_char(t.create_time,'mi'),2,1)<5 then to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||0
         else to_char(t.create_time,'yyyymmddhh24')||substr(to_char(t.create_time,'mi'),1,1)||5 end as newTime -- time period pseudo-column    
  from acquire_order t
  where t.create_time>=to_date('20160406000000','yyyymmddhh24miss')
  order by t.create_time asc
) tmp
group by tmp.newTime
order by totalNum desc

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326703336&siteId=291194637