Implementation of DB2 circular Query

Source: Internet
Author: User

DB2 circular query is a common problem. If we encounter a large table problem, how can we implement Circular query for large tables? The following describes how to implement the Circular query of DB2 in large tables for your reference.

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:

 
 
  1. -- Original data table
  2. Create table zhangmo_online (sid integer, logintime timestamp, logouttime timestamp );
  3.  
  4. -- Test Data
  5. Insert into zhangmo_online
  6. Select 51982112, current timestamp, current timestamp + 20 MINUTES from (values (1);
  7.  
  8. -- Create a sampling table for a five-minute period
  9. Create table zhangmo_onlinenum (onlinetime varchar (20), onlinenum integer );
  10.  
  11. -- Create a sample pomelo Process
  12. Create procedure zhangmo_onlinenum ()
  13. Language SQL
  14. Begin
  15. Declare I timestamp;
  16. Set I = TIMESTAMP ('2017-02-01 00:00:00 ');
  17. While I
  18. Insert into zhangmo_onlinenum (onlinetime, onlinenum)
  19. Select trim (char (year (I) |
  20. Right ('00' | trim (char (month (I), 2) |
  21. Right ('00' | trim (char (day (I), 2) |
  22. Right ('00' | trim (char (hour (I), 2) |
  23. Right ('00' | trim (char (minute (I), 2), count (distinct sid)
  24. From zhangmo_online
  25. Where logintime I;
  26. Set ii = I + 5 MINUTES;
  27. End while;
  28. Commit;
  29. End;
  30.  
  31. -- Execution Process
  32. Call zhangmo_onlinenum ();
  33.  
  34. -- Get the final data
  35. Select left (onlinetime, 10), max (onlinenum) from zhangmo_onlinenum
  36. Group by left (onlinetime, 10 );
  37.  

DB2 query Management Utility

DB2 Data Warehouse Product Introduction

Implementation of DB2 Paging

DB2 cursor and usage

How to manage DB2 tablespaces

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.