|
| 1 | +# [3126. Server Utilization Time](https://leetcode.cn/problems/server-utilization-time) |
| 2 | + |
| 3 | +[English Version](/solution/3100-3199/3126.Server%20Utilization%20Time/README_EN.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## 题目描述 |
| 8 | + |
| 9 | +<!-- 这里写题目描述 --> |
| 10 | + |
| 11 | +<p>Table: <code>Servers</code></p> |
| 12 | + |
| 13 | +<pre> |
| 14 | ++----------------+----------+ |
| 15 | +| Column Name | Type | |
| 16 | ++----------------+----------+ |
| 17 | +| server_id | int | |
| 18 | +| status_time | datetime | |
| 19 | +| session_status | enum | |
| 20 | ++----------------+----------+ |
| 21 | +(server_id, status_time, session_status) is the primary key (combination of columns with unique values) for this table. |
| 22 | +session_status is an ENUM (category) type of ('start', 'stop'). |
| 23 | +Each row of this table contains server_id, status_time, and session_status. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p>Write a solution to find the <strong>total time</strong> when servers were <strong>running</strong>. The output should be in units of <strong>full days</strong>.</p> |
| 27 | + |
| 28 | +<p>Return <em>the result table in <strong>any</strong></em><em> order.</em></p> |
| 29 | + |
| 30 | +<p>The result format is in the following example.</p> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong class="example">Example:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block"> |
| 36 | +<p><strong>Input:</strong></p> |
| 37 | + |
| 38 | +<p>Servers table:</p> |
| 39 | + |
| 40 | +<pre class="example-io"> |
| 41 | ++-----------+---------------------+----------------+ |
| 42 | +| server_id | status_time | session_status | |
| 43 | ++-----------+---------------------+----------------+ |
| 44 | +| 3 | 2023-11-04 16:29:47 | start | |
| 45 | +| 3 | 2023-11-05 01:49:47 | stop | |
| 46 | +| 3 | 2023-11-25 01:37:08 | start | |
| 47 | +| 3 | 2023-11-25 03:50:08 | stop | |
| 48 | +| 1 | 2023-11-13 03:05:31 | start | |
| 49 | +| 1 | 2023-11-13 11:10:31 | stop | |
| 50 | +| 4 | 2023-11-29 15:11:17 | start | |
| 51 | +| 4 | 2023-11-29 15:42:17 | stop | |
| 52 | +| 4 | 2023-11-20 00:31:44 | start | |
| 53 | +| 4 | 2023-11-20 07:03:44 | stop | |
| 54 | +| 1 | 2023-11-20 00:27:11 | start | |
| 55 | +| 1 | 2023-11-20 01:41:11 | stop | |
| 56 | +| 3 | 2023-11-04 23:16:48 | start | |
| 57 | +| 3 | 2023-11-05 01:15:48 | stop | |
| 58 | +| 4 | 2023-11-30 15:09:18 | start | |
| 59 | +| 4 | 2023-11-30 20:48:18 | stop | |
| 60 | +| 4 | 2023-11-25 21:09:06 | start | |
| 61 | +| 4 | 2023-11-26 04:58:06 | stop | |
| 62 | +| 5 | 2023-11-16 19:42:22 | start | |
| 63 | +| 5 | 2023-11-16 21:08:22 | stop | |
| 64 | ++-----------+---------------------+----------------+ |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p><strong>Output:</strong></p> |
| 68 | + |
| 69 | +<pre class="example-io"> |
| 70 | ++-------------------+ |
| 71 | +| total_uptime_days | |
| 72 | ++-------------------+ |
| 73 | +| 1 | |
| 74 | ++-------------------+ |
| 75 | +</pre> |
| 76 | + |
| 77 | +<p><strong>Explanation:</strong></p> |
| 78 | + |
| 79 | +<ul> |
| 80 | + <li>For server ID 3: |
| 81 | + <ul> |
| 82 | + <li>From 2023-11-04 16:29:47 to 2023-11-05 01:49:47: ~9.3 hours</li> |
| 83 | + <li>From 2023-11-25 01:37:08 to 2023-11-25 03:50:08: ~2.2 hours</li> |
| 84 | + <li>From 2023-11-04 23:16:48 to 2023-11-05 01:15:48: ~1.98 hours</li> |
| 85 | + </ul> |
| 86 | + Total for server 3: ~13.48 hours</li> |
| 87 | + <li>For server ID 1: |
| 88 | + <ul> |
| 89 | + <li>From 2023-11-13 03:05:31 to 2023-11-13 11:10:31: ~8 hours</li> |
| 90 | + <li>From 2023-11-20 00:27:11 to 2023-11-20 01:41:11: ~1.23 hours</li> |
| 91 | + </ul> |
| 92 | + Total for server 1: ~9.23 hours</li> |
| 93 | + <li>For server ID 4: |
| 94 | + <ul> |
| 95 | + <li>From 2023-11-29 15:11:17 to 2023-11-29 15:42:17: ~0.52 hours</li> |
| 96 | + <li>From 2023-11-20 00:31:44 to 2023-11-20 07:03:44: ~6.53 hours</li> |
| 97 | + <li>From 2023-11-30 15:09:18 to 2023-11-30 20:48:18: ~5.65 hours</li> |
| 98 | + <li>From 2023-11-25 21:09:06 to 2023-11-26 04:58:06: ~7.82 hours</li> |
| 99 | + </ul> |
| 100 | + Total for server 4: ~20.52 hours</li> |
| 101 | + <li>For server ID 5: |
| 102 | + <ul> |
| 103 | + <li>From 2023-11-16 19:42:22 to 2023-11-16 21:08:22: ~1.43 hours</li> |
| 104 | + </ul> |
| 105 | + Total for server 5: ~1.43 hours</li> |
| 106 | +</ul> |
| 107 | +The accumulated runtime for all servers totals approximately 44.46 hours, equivalent to one full day plus some additional hours. However, since we consider only full days, the final output is rounded to 1 full day.</div> |
| 108 | + |
| 109 | +## 解法 |
| 110 | + |
| 111 | +### 方法一 |
| 112 | + |
| 113 | +<!-- tabs:start --> |
| 114 | + |
| 115 | +```sql |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +<!-- tabs:end --> |
| 120 | + |
| 121 | +<!-- end --> |
0 commit comments