Oracle calculates continuous landing/working days

Source: Internet
Author: User

There is now a report that calculates the number of days a user is working continuously, and finds that the problem can be calculated perfectly by using the Row_number analysis function.

This SQL can solve the problem of computing users ' continuous landing, sign-in, work, absenteeism and so on.

    1. First, sort the row_number by date.

    2. The day of the date, such as 2016-7-1, is truncated to 1 and converted into numbers.

    3. This date number minus Row_number calculated value, as the group number, because the discontinuous value of the group number is inconsistent;

    4. Group by based on the group Number field, you can calculate the start, end time, and number of days for each successive work


As the original data is like this

2016/7/1

2016/7/2

2016/7/4

2016/7/5

2016/7/6

2016/7/7

......



The calculated result is

Group number start time end time number of days

0 2016/7/1 2016/7/2 2

1 2016/7/4 2016/7/9 6

......



The code below

With t1 as (select date ' 2016-7-1 '  d1 from dualunionselect date ' 2016-7-2 '  d1 from dualunionselect date ' 2016-7-4 '  d1 from dualunionselect date ' 2016-7-5 '  d1 from dualunionselect date ' 2016-7-6 '  d1 from dualunionselect  date ' 2016-7-7 '  d1 from dualunionselect date ' 2016-7-8 '  d1 from  Dualunionselect date ' 2016-7-9 '  d1 from dualunionselect date ' 2016-7-11 '  d1  From dualunionselect date ' 2016-7-12 '  d1 from dualunionselect date ' 2016-7-13 '  d1 from dualunionselect date ' 2016-7-14 '  d1 from dualunionselect date ' 2016-7-18 '  d1 from dualunionselect date ' 2016-7-19 '  d1 from dual) Select  gn,min (D1), Max (D1), COUNT (*)  from (Select d1,to_number (To_char (d1, ' DD '))-row_number ()  over ( ORDER BY D1)  gn  from t1) group by gn order by 2 


This article is from "Richard's notes-Cheng" blog, make sure to keep this source http://zxf261.blog.51cto.com/701797/1828286

Oracle calculates continuous landing/working days

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.