Skip to content
Advertisement

SQL – % Breakout by Hour

I’m trying to write a query that will break out what percentage of time is utilized within a hour given a time range.

Sample data:

Looking to get output like this:

expected output

Any help would greatly be appreciated.

Advertisement

Answer

By using an ad-hoc tally table to create every minute of the day. This assumes you are processing to the minute. Then it becomes a small matter of the conditional aggregation.

Example

Returns

enter image description here

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