Skip to content
Advertisement

Get start and end date per group and sequence, derived by one date column

I have the data below:

The StartDate and EndDate are always identical and they are only working days (Monday to Friday). The WeekDayName is the name of the StartDate. ID is an integer. A_Type is (here) always 430. A_Days (always lower than or equal to 1), A_Hours (always lower than or equal to 8.20).

The desired output is for each ID a StartDate, EndDate, Sum of A_Days, Sum of A_Hours, SumDays. SumDays is the number of working days between StartDate and EndDate. EndDate is the day that which is the last date of a “block” of following dates, grouped by the ID.

E.g.

150041 has two records, since there are two “blocks” of following dates. The first, 2019-01-17, and the seconds from 2019-08-20 to 2019-08-21.

Could someone please help me with that? (As a bonus, it would be great if it is possible to be able to group by the column A_Type as well). I could not get it working.

Any help is appreciated.

Thank you in advance,

Best

Advertisement

Answer

I understand that you want to group togethers records that have the same id and adjacent days (ignoring week-ends). This is a gaps and islands problem.

Here is one approach that uses lag() to retrieve the previous endDate and compare it to the current startDate; everytime dates are not “adjacent”, a new group starts (a little additional logic is needed to handle the Friday > Monday gap).

For your sample data, this produces:

id     | startDate               | endDate                 | a_days | a_hours | sumDays
:----- | :---------------------- | :---------------------- | :----- | :------ | ------:
150017 | 2019-12-23 00:00:00.000 | 2019-12-24 00:00:00.000 | 2.00   | 12.30   |       2
150017 | 2019-12-27 00:00:00.000 | 2019-12-31 00:00:00.000 | 3.00   | 20.50   |       3
150035 | 2019-03-12 00:00:00.000 | 2019-03-12 00:00:00.000 | 0.66   | 5.45    |       1
150041 | 2019-01-17 00:00:00.000 | 2019-01-17 00:00:00.000 | 1.00   | 8.20    |       1
150041 | 2019-08-20 00:00:00.000 | 2019-08-21 00:00:00.000 | 2.00   | 16.40   |       2
150045 | 2019-05-13 00:00:00.000 | 2019-05-15 00:00:00.000 | 3.00   | 24.60   |       3
150045 | 2019-11-25 00:00:00.000 | 2019-11-29 00:00:00.000 | 5.00   | 41.00   |       5
150046 | 2019-03-11 00:00:00.000 | 2019-03-11 00:00:00.000 | 1.00   | 8.20    |       1
150048 | 2019-10-08 00:00:00.000 | 2019-10-08 00:00:00.000 | 0.30   | 2.50    |       1
150048 | 2019-10-28 00:00:00.000 | 2019-10-28 00:00:00.000 | 1.00   | 8.20    |       1
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement