Oracle 5-minute or 30-minute segmentation method

Source: Internet
Author: User
Tags time interval

In recent projects, there is a customer requirement that is divided into a total number of users every 5 minutes for all time points per day.

The data scenarios are:

A game in which all users online time data (of course simple summation, may have duplicate data). But in this focus is the method used by Oracle SQL to split at a certain time interval, the specific 5 minute split instance is as follows:

SELECT Tt.reasoncontent,to_char (tt.day_id, ' Hh24:mi ') daytime, tt.num from (
SELECT ll.day_id,ll.reasoncontent,count (*) num from (
SELECT D.day_id,dd.logtime,dd.groupname,dd.userid,dd.reasoncontent from (
SELECT I.logtime,i.gameid,i.groupname,i.userid,i.reason,case when dic.key_id was NULL then ' other reason ' else Dic.key_value END R Easoncontent from
Table I left JOIN
Tabledic dic on i.reason=dic.key_id) DD,
(SELECT to_date (' 2014-09-20 00:00:00 ', ' yyyy-mm-dd hh24:mi:ss ') + (1/24/60 * * (ROWNUM-1)) day_id from DUAL
CONNECT by ROWNUM <= 288) D WHERE d.day_id-(1/24/60 *) <= DD. LogTime and d.day_id >= DD. LogTime) ll GROUP by Ll.day_id,ll.reasoncontent ORDER by ll.day_id) TT

Key code:

(SELECT to_date (' 2014-09-20 00:00:00 ', ' yyyy-mm-dd hh24:mi:ss ') + (1/24/60 * 5 * (ROWNUM-1)) day_id from DUAL
CONNECT by ROWNUM <= 288) D WHERE d.day_id-(1/24/60 * 5) <= DD. LogTime and d.day_id >= DD. LogTime

Explanation: There are 24 hours a day and there are 288 5-minute points. Incremental segmentation is achieved by rownum combined with connect by.

If the increment is 5 minutes, then

(1/24/60 * 5 * (ROWNUM-1))

If the increment is 30 minutes, then

(1/24/60 * * (ROWNUM-1))

CONNECT by is a hierarchical query clause for Oracle, typically used for queries of tree or hierarchical result sets.

This article is from "Happy Chickadee" blog, please make sure to keep this source http://1723824.blog.51cto.com/1713824/1559407

Oracle 5-minute or 30-minute segmentation method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.