Skip to content
Advertisement

SQL Calculate Consecutive hours [closed]

Need to calculate consecutive hours.

Here is the data

This the client has a limit of the amount of consecutive hours allowed.

Here is the result I’m looking for

Or a formula to calculate if client exceeds the allowed consecutive hours.

Advertisement

Answer

This is a type of gaps-and-islands problem. Because you are dealing with minutes, it makes more sense to me to accumulate minutes rather than hours. You can divide by 60 to get hours:

EDIT:

To handle counting hours across days is really just tweaking the above query:

Here is a db<>fiddle.

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