DB2 circular query is a frequently used query method. The following describes the implementation process of circular query in DB2. If you are interested in this aspect, take a look.
Requirement Description:
There is a table with three fields, which are the departure time of the flight signal Logon Time.
Now we want to obtain the number of simultaneous users by day.
Our method is to divide the time into a time point every five minutes, and calculate the number of users who went online before the time point and went offline after the time point.
Then, it takes the most online users a day. The number of simultaneous online users for the day.
The implementation of DB2 cyclic query is as follows:
-- Original data table
- create table zhangmo_online(sid integer,logintime timestamp,logouttime timestamp);
-- Test Data
- insert into zhangmo_online
- select 51982112,current timestamp,current timestamp+20 MINUTES from (values(1)) a;
-- Create a sampling table for a five-minute period
- create table zhangmo_onlinenum(onlinetime varchar(20),onlinenum integer);
-- Create a sample pomelo Process
- create procedure zhangmo_onlinenum()
- language sql
- begin
- declare i timestamp;
- set i=TIMESTAMP ('2008-02-01 00:00:00');
- while i<TIMESTAMP ('2008-03-01 00:00:00') do
- insert into zhangmo_onlinenum(onlinetime,onlinenum)
- select trim(char(year(i)))||
- right('00'||trim(char(month(i))),2)||
- right('00'||trim(char(day(i))),2)||
- right('00'||trim(char(hour(i))),2)||
- right('00'||trim(char(minute(i))),2),count(distinct sid)
- from zhangmo_online
- where logintime<i and logouttime>i;
- set ii=i+5 MINUTES;
- end while;
- commit;
- end;
-- Execution Process
- call zhangmo_onlinenum();
-- Get the final data
- select left(onlinetime,10),max(onlinenum) from zhangmo_onlinenum
- group by left(onlinetime,10);
Rollback of A DB2 partitioned Database
Implementation of DB2 partitioned Database Backup
DB2 logfilsiz parameter settings
Online Implementation of changing the DB2 page size
Silent state of the DB2 tablespace