Skip to content
Advertisement

Calculate working hours between exit date and 3 months before exit date bigquery sql

I’m trying to calculate the total working hours between two dates in bigquery sql: The dates being between MAX(date) and DATE_SUB(MAX(date), interval 3 month). In other words, I want to know the sum of working hours between the exit date and 3 months prior to the exit date.

The current table is something like this:

id date hours
abc 2020-10-01 12
abc 2020-12-07 4
abc 2020-12-12 12
abc 2020-12-25 6
abc 2021-01-07 9
abc 2021-02-04 7

The ideal output is:

id hours
abc 38

I have multiple workers and workers have different working dates and hours.

Advertisement

Answer

We need a subquery here to calculate exit_date first:

enter image description here

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