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.
First, sort the row_number by date.
The day of the date, such as 2016-7-1, is truncated to 1 and converted into numbers.
This date number minus Row_number calculated value, as the group number, because the discontinuous value of the group number is inconsistent;
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