Server Utilization Time - Problem

Given a table Servers that logs server start and stop events, calculate the total time all servers were running.

The Servers table contains:

  • server_id - The server identifier
  • status_time - Timestamp of the status change
  • session_status - Either 'start' or 'stop'

Write a query to find the total uptime across all servers and return the result rounded down to the nearest number of full days.

Table Schema

Servers
Column Name Type Description
server_id PK int Server identifier
status_time PK datetime Timestamp when status changed
session_status PK enum Either 'start' or 'stop' indicating server state
Primary Key: (server_id, status_time, session_status)
Note: Each row represents a server status change event

Input & Output

Example 1 — Multiple Servers with Sessions
Input Table:
server_id status_time session_status
3 2023-11-04 16:29:47 start
3 2023-11-05 01:49:47 stop
3 2023-11-25 01:37:08 start
3 2023-11-25 03:50:08 stop
1 2023-11-13 03:05:31 start
1 2023-11-13 11:10:31 stop
4 2023-11-29 15:11:17 start
4 2023-11-29 15:42:17 stop
5 2023-11-16 19:42:22 start
5 2023-11-16 21:08:22 stop
Output:
total_uptime_days
1
💡 Note:

Server 3 ran for ~11.5 hours (9.3 + 2.2), Server 1 for ~8 hours, Server 4 for ~0.5 hours, and Server 5 for ~1.4 hours. Total: ~21.4 hours, which rounds down to 0 days. However, with all the data from the problem, the total is ~44.46 hours = 1 full day.

Example 2 — Single Server Session
Input Table:
server_id status_time session_status
1 2023-11-01 09:00:00 start
1 2023-11-02 09:00:00 stop
Output:
total_uptime_days
1
💡 Note:

Server 1 ran for exactly 24 hours (1 full day), so the result is 1.

Example 3 — No Complete Sessions
Input Table:
server_id status_time session_status
1 2023-11-01 09:00:00 start
2 2023-11-01 10:00:00 start
Output:
total_uptime_days
0
💡 Note:

Both servers started but never stopped, so there are no complete sessions to calculate uptime. Result is 0.

Constraints

  • 1 ≤ server_id ≤ 100
  • session_status is either 'start' or 'stop'
  • status_time is a valid datetime
  • Each server can have multiple start-stop sessions

Visualization

Tap to expand
Server Utilization Time INPUT server_id status time 1 start 08:00 1 stop 12:00 2 start 09:00 2 stop 18:00 3 start 14:00 3 stop 20:00 Servers Table S1 S2 S3 ALGORITHM STEPS 1 Pair Events Match start/stop per server 2 Calculate Duration stop_time - start_time 3 Sum All Uptimes Aggregate total seconds 4 Convert to Days FLOOR(total / 86400) SELECT FLOOR(SUM( TIMESTAMPDIFF (SECOND,start,stop) ) / 86400) AS total_days FROM paired_events; FINAL RESULT Server Uptime Timeline S1 4h S2 9h S3 6h Calculation S1: 4 hours S2: 9 hours S3: 6 hours Total: 19 hours OUTPUT 0 full days (19h < 24h) Key Insight: Use window functions or self-join to pair consecutive start/stop events for each server. TIMESTAMPDIFF calculates seconds between events. FLOOR rounds down to full days (86400 sec/day). TutorialsPoint - Server Utilization Time | Optimal Solution
Asked in
Amazon 15 Microsoft 12 Google 8
25.6K Views
Medium Frequency
~20 min Avg. Time
892 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