|
| 1 | +# [1934. Confirmation Rate](https://leetcode-cn.com/problems/confirmation-rate) |
| 2 | + |
| 3 | +[English Version](/solution/1900-1999/1934.Confirmation%20Rate/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Table: <code>Signups</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++----------------+----------+ |
| 13 | +| Column Name | Type | |
| 14 | ++----------------+----------+ |
| 15 | +| user_id | int | |
| 16 | +| time_stamp | datetime | |
| 17 | ++----------------+----------+ |
| 18 | +user_id is the primary key for this table. |
| 19 | +Each row contains information about the signup time for the user with ID user_id. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | + |
| 24 | +<p>Table: <code>Confirmations</code></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | ++----------------+----------+ |
| 28 | +| Column Name | Type | |
| 29 | ++----------------+----------+ |
| 30 | +| user_id | int | |
| 31 | +| time_stamp | datetime | |
| 32 | +| action | ENUM | |
| 33 | ++----------------+----------+ |
| 34 | +(user_id, time_stamp) is the primary key for this table. |
| 35 | +user_id is a foreign key with a reference to the Signups table. |
| 36 | +action is an ENUM of the type ('confirmed', 'timeout') |
| 37 | +Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed ('confirmed') or expired without confirming ('timeout').</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | + |
| 41 | +<p>The <strong>confirmation rate</strong> of a user is the number of <code>'confirmed'</code> messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is <code>0</code>.</p> |
| 42 | + |
| 43 | +<p>Write an SQL query to find the <strong>confirmation rate</strong> of each user.</p> |
| 44 | + |
| 45 | +<p>Return the result table <strong>in any order</strong>.</p> |
| 46 | + |
| 47 | +<p>The query result format is in the following example:</p> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | + |
| 51 | +<pre> |
| 52 | +Signups table: |
| 53 | ++---------+---------------------+ |
| 54 | +| user_id | time_stamp | |
| 55 | ++---------+---------------------+ |
| 56 | +| 3 | 2020-03-21 10:16:13 | |
| 57 | +| 7 | 2020-01-04 13:57:59 | |
| 58 | +| 2 | 2020-07-29 23:09:44 | |
| 59 | +| 6 | 2020-12-09 10:39:37 | |
| 60 | ++---------+---------------------+ |
| 61 | + |
| 62 | +Confirmations table: |
| 63 | ++---------+---------------------+-----------+ |
| 64 | +| user_id | time_stamp | action | |
| 65 | ++---------+---------------------+-----------+ |
| 66 | +| 3 | 2021-01-06 03:30:46 | timeout | |
| 67 | +| 3 | 2021-07-14 14:00:00 | timeout | |
| 68 | +| 7 | 2021-06-12 11:57:29 | confirmed | |
| 69 | +| 7 | 2021-06-13 12:58:28 | confirmed | |
| 70 | +| 7 | 2021-06-14 13:59:27 | confirmed | |
| 71 | +| 2 | 2021-01-22 00:00:00 | confirmed | |
| 72 | +| 2 | 2021-02-28 23:59:59 | timeout | |
| 73 | ++---------+---------------------+-----------+ |
| 74 | + |
| 75 | +Result table |
| 76 | ++---------+-------------------+ |
| 77 | +| user_id | confirmation_rate | |
| 78 | ++---------+-------------------+ |
| 79 | +| 6 | 0.00 | |
| 80 | +| 3 | 0.00 | |
| 81 | +| 7 | 1.00 | |
| 82 | +| 2 | 0.50 | |
| 83 | ++---------+-------------------+ |
| 84 | + |
| 85 | +User 6 did not request any confirmation messages. The confirmation rate is 0. |
| 86 | +User 3 made 2 requests and both timed out. The confirmation rate is 0. |
| 87 | +User 7 made 3 requests and all were confirmed. The confirmation rate is 1. |
| 88 | +User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is 1 / 2 = 0.5. |
| 89 | +</pre> |
| 90 | + |
| 91 | + |
| 92 | +## 解法 |
| 93 | + |
| 94 | +<!-- 这里可写通用的实现逻辑 --> |
| 95 | + |
| 96 | +<!-- tabs:start --> |
| 97 | + |
| 98 | +### **SQL** |
| 99 | + |
| 100 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 101 | + |
| 102 | +```sql |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +<!-- tabs:end --> |
0 commit comments