Server Utilization Time - Problem

You are working as a DevOps engineer for a cloud hosting company that needs to calculate billing based on server usage. Your task is to analyze server activity logs to determine the total time servers were actively running.

Given a database table Servers that logs every time a server starts or stops, you need to calculate the total uptime across all servers and convert it to full days (rounded down).

Key Requirements:

  • Each server can have multiple start/stop sessions
  • Sessions are properly paired (every start has a corresponding stop)
  • Calculate total runtime across ALL servers
  • Return result as full days only (floor the result)

Example: If servers ran for 25.7 hours total, that equals 1.07 days, which rounds down to 1 full day.

Input & Output

example_1.sql โ€” Basic Server Sessions
$ Input: Servers table with multiple server sessions: server_id=1: start at 03:05:31, stop at 11:10:31 (8 hours) server_id=3: start at 16:29:47, stop at 01:49:47 next day (9.3 hours)
โ€บ Output: total_uptime_days: 0
๐Ÿ’ก Note: Total runtime is 17.3 hours, which equals 0.72 days. Since we floor the result, it becomes 0 full days.
example_2.sql โ€” Extended Sessions
$ Input: Multiple servers with longer sessions: server_id=1: 8 + 1.2 = 9.2 hours server_id=3: 9.3 + 2.2 + 2.0 = 13.5 hours server_id=4: 6.5 + 0.5 + 5.7 + 7.8 = 20.5 hours server_id=5: 1.4 hours
โ€บ Output: total_uptime_days: 1
๐Ÿ’ก Note: Total runtime is 44.6 hours = 1.86 days. Floored to 1 full day.
example_3.sql โ€” Edge Case - Short Sessions
$ Input: Multiple very short server sessions: server_id=1: 0.5 hours server_id=2: 1.0 hours server_id=3: 0.3 hours
โ€บ Output: total_uptime_days: 0
๐Ÿ’ก Note: Total runtime is 1.8 hours = 0.075 days. Floored to 0 full days.

Constraints

  • 1 โ‰ค number of server records โ‰ค 104
  • Each server has properly paired start/stop events
  • status_time is a valid datetime format
  • session_status is either 'start' or 'stop'
  • No overlapping sessions for the same server
  • All timestamps are in chronological order per server

Visualization

Tap to expand
Server Session Pairing ProcessRaw Server LogsS1E3S3E1S1E1Mixed server eventsS=Start, E=EndGrouped by ServerServer 1Server 3Partition by server_idPaired Sessions8.1h1.2hCalculate durationfor each pairFinal CalculationSum all session durations: 44.6 hoursConvert to days: 44.6 รท 24 = 1.86 daysFloor result: FLOOR(1.86) = 1 full day
Understanding the Visualization
1
Group by Server
Separate all events by server_id, like sorting parking tickets by license plate
2
Sequence Events
Number start and stop events chronologically for each server
3
Pair Sessions
Match start event #1 with stop event #1, start #2 with stop #2, etc.
4
Calculate Duration
Compute time difference for each session pair
5
Aggregate & Convert
Sum all durations and convert to full days using FLOOR function
Key Takeaway
๐ŸŽฏ Key Insight: Window functions enable efficient session pairing by partitioning data by server and sequencing events chronologically, avoiding expensive nested queries while maintaining optimal performance.
Asked in
Amazon 85 Google 72 Microsoft 58 Meta 41
68.2K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen