|
11 | 11 |
|
12 | 12 | ## [1336. Number of Transactions per Visit (Medium)](https://leetcode.com/problems/number-of-transactions-per-visit "")
|
13 | 13 |
|
| 14 | +<p>Table: <code>Visits</code></p> |
| 15 | +<pre> |
| 16 | ++---------------+---------+ |
| 17 | +| Column Name | Type | |
| 18 | ++---------------+---------+ |
| 19 | +| user_id | int | |
| 20 | +| visit_date | date | |
| 21 | ++---------------+---------+ |
| 22 | +(user_id, visit_date) is the primary key for this table. |
| 23 | +Each row of this table indicates that user_id has visited the bank in visit_date. |
| 24 | +</pre> |
14 | 25 |
|
| 26 | +<p>Table: <code>Transactions</code></p> |
| 27 | +<pre> |
| 28 | ++------------------+---------+ |
| 29 | +| Column Name | Type | |
| 30 | ++------------------+---------+ |
| 31 | +| user_id | int | |
| 32 | +| transaction_date | date | |
| 33 | +| amount | int | |
| 34 | ++------------------+---------+ |
| 35 | +There is no primary key for this table, it may contain duplicates. |
| 36 | +Each row of this table indicates that user_id has done a transaction of amount in transaction_date. |
| 37 | +It is guaranteed that the user has visited the bank in the transaction_date.(i.e The Visits table contains (user_id, transaction_date) in one row) |
| 38 | +</pre> |
| 39 | + |
| 40 | +Write an SQL query to find how many users visited the bank and didn't do any transactions, how many visited the bank and did one transaction and so on. |
| 41 | + |
| 42 | +The result table will contain two columns transactions_count which is the number of transactions done in one visit and visits_count which is the corresponding number of users who did transactions_count in one visit to the bank. transactions_count should take all values from 0 to max(transactions_count) done by one or more users. |
| 43 | + |
| 44 | +Order the result table by transactions_count. |
| 45 | + |
| 46 | +The query result format is in the following example: |
| 47 | + |
| 48 | +<pre> |
| 49 | +Visits table: |
| 50 | ++---------+------------+ |
| 51 | +| user_id | visit_date | |
| 52 | ++---------+------------+ |
| 53 | +| 1 | 2020-01-01 | |
| 54 | +| 2 | 2020-01-02 | |
| 55 | +| 12 | 2020-01-01 | |
| 56 | +| 19 | 2020-01-03 | |
| 57 | +| 1 | 2020-01-02 | |
| 58 | +| 2 | 2020-01-03 | |
| 59 | +| 1 | 2020-01-04 | |
| 60 | +| 7 | 2020-01-11 | |
| 61 | +| 9 | 2020-01-25 | |
| 62 | +| 8 | 2020-01-28 | |
| 63 | ++---------+------------+ |
| 64 | +Transactions table: |
| 65 | ++---------+------------------+--------+ |
| 66 | +| user_id | transaction_date | amount | |
| 67 | ++---------+------------------+--------+ |
| 68 | +| 1 | 2020-01-02 | 120 | |
| 69 | +| 2 | 2020-01-03 | 22 | |
| 70 | +| 7 | 2020-01-11 | 232 | |
| 71 | +| 1 | 2020-01-04 | 7 | |
| 72 | +| 9 | 2020-01-25 | 33 | |
| 73 | +| 9 | 2020-01-25 | 66 | |
| 74 | +| 8 | 2020-01-28 | 1 | |
| 75 | +| 9 | 2020-01-25 | 99 | |
| 76 | ++---------+------------------+--------+ |
| 77 | +Result table: |
| 78 | ++--------------------+--------------+ |
| 79 | +| transactions_count | visits_count | |
| 80 | ++--------------------+--------------+ |
| 81 | +| 0 | 4 | |
| 82 | +| 1 | 5 | |
| 83 | +| 2 | 0 | |
| 84 | +| 3 | 1 | |
| 85 | ++--------------------+--------------+ |
| 86 | +Users 1, 2, 12 and 19 visited the bank in 2020-01-01, 2020-01-02, 2020-01-01 and 2020-01-03 respectively, and didn't do any transactions. |
| 87 | +So we have visits_count = 4 for transactions_count = 0. |
| 88 | +Users 2, 7 and 8 visited the bank in 2020-01-03, 2020-01-11 and 2020-01-28 respectively, and did one transaction. |
| 89 | +User 1 Also visited the bank in 2020-01-02 and 2020-01-04 and did one transaction each day. |
| 90 | +So we have total visits_count = 5 for transactions_count = 1. |
| 91 | +For transactions_count = 2 we don't have any users who visited the bank and did two transactions. |
| 92 | +For transactions_count = 3 we have user 9 who visited the bank in 2020-01-25 and did three transactions. |
| 93 | +Note that we stopped at transactions_count = 3 as this is the maximum number of transactions done by one user in one visit to the bank. |
| 94 | +</pre> |
0 commit comments