Skip to content
Advertisement

Big Query – Calculate start and end date back to back

I have a problem, which I need some advise, I am required to calculate the number of leave calendar days taken back-to-back on big query. (For eg. 2 leave records taken on 07-01-2020 to 10-01-2020 and 13-01-2020 to 15-01-2020, should return 07-01-2020 to 15-01-2020)

However, there are certain weeks, where leave is taken at 3/4 days gap because there is public holiday on that week. Can anyone suggest a possible work around to this? I created a table for public holidays but I am stuck with how I can possible considers weeks with public holiday as back-to-back. I considered window function but I am not sure what is the correct logic.

Original data set

personnel_number start_date end_date next_start_date next_end_date days_between_next_row remarks
100100 16/1/2020 17/1/2020 20/1/2020 24/1/2020 3
100100 20/1/2020 24/1/2020 28/1/2020 31/1/2020 4 “public holiday on 27-Jan”
100100 28/1/2020 31/1/2020 10/2/2020 13/2/2020 10
100100 10/2/2020 13/2/2020 NULL NULL

Public Holiday Table

pub_start_date pub_end_date remarks
25/1/2020 27/1/2020 “CNY Holiday”

Desired outcome

personnel_number start_date back_to_back_end_date
100100 16/1/2020 31/1/2020
100100 10/2/2020 13/2/2020

Advertisement

Answer

Below is for BigQuery Standard SQL

if to apply to sample data from your question

output is

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement