高级农民
- 积分
- 4176
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
1336. Number of Transactions per Visit
Hard
Topics
SQL Schema
Pandas Schema
Table: Visits
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| visit_date | date |
+---------------+---------+
(user_id, visit_date) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that user_id has visited the bank in visit_date.
Table: Transactions
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| user_id | int |
| transaction_date | date |
| amount | int |
+------------------+---------+
This table may contain duplicates rows.
Each row of this table indicates that user_id has done a transaction of amount in transaction_date.
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)
A bank wants to draw a chart of the number of transactions bank visitors did in one visit to the bank and the corresponding number of visitors who have done this number of transaction in one visit.
Write a solution 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.
The result table will contain two columns:
transactions_count which is the number of transactions done in one visit.
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.
Return the result table ordered by transactions_count.
The result format is in the following example.
Example 1:
Input:
Visits table:
+---------+------------+
| user_id | visit_date |
+---------+------------+
| 1 | 2020-01-01 |
| 2 | 2020-01-02 |
| 12 | 2020-01-01 |
| 19 | 2020-01-03 |
| 1 | 2020-01-02 |
| 2 | 2020-01-03 |
| 1 | 2020-01-04 |
| 7 | 2020-01-11 |
| 9 | 2020-01-25 |
| 8 | 2020-01-28 |
+---------+------------+
Transactions table:
+---------+------------------+--------+
| user_id | transaction_date | amount |
+---------+------------------+--------+
| 1 | 2020-01-02 | 120 |
| 2 | 2020-01-03 | 22 |
| 7 | 2020-01-11 | 232 |
| 1 | 2020-01-04 | 7 |
| 9 | 2020-01-25 | 33 |
| 9 | 2020-01-25 | 66 |
| 8 | 2020-01-28 | 1 |
| 9 | 2020-01-25 | 99 |
+---------+------------------+--------+
Output:
+--------------------+--------------+
| transactions_count | visits_count |
+--------------------+--------------+
| 0 | 4 |
| 1 | 5 |
| 2 | 0 |
| 3 | 1 |
+--------------------+--------------+
Explanation: The chart drawn for this example is shown above.
* For transactions_count = 0, The visits (1, "2020-01-01"), (2, "2020-01-02"), (12, "2020-01-01") and (19, "2020-01-03") did no transactions so visits_count = 4.
* For transactions_count = 1, The visits (2, "2020-01-03"), (7, "2020-01-11"), (8, "2020-01-28"), (1, "2020-01-02") and (1, "2020-01-04") did one transaction so visits_count = 5.
* For transactions_count = 2, No customers visited the bank and did two transactions so visits_count = 0.
* For transactions_count = 3, The visit (9, "2020-01-25") did three transactions so visits_count = 1.
* For transactions_count >= 4, No customers visited the bank and did more than three transactions so we will stop at transactions_count = 3
思路
题目问的是"每一次访问做了几笔交易"的分布,也就是一张直方图。注意统计单位是一次访问,即 (user_id, visit_date) 这个组合,不是一个用户。
算出每次访问的交易笔数:用 Visits LEFT JOIN Transactions,按 (user_id, visit_date) 匹配。没有交易的访问也要保留,笔数是 0。
生成 0 到最大笔数的所有整数:中间可能有空缺,如示例里没有"交易 2 笔"的访问,但 transactions_count = 2 这一行仍然要输出,visits_count 为 0。所以需要一张 0 到 max 的数字表作为主表。
统计每个笔数对应的访问数:用数字表 LEFT JOIN 第 1 步的结果,数出有多少次访问。- # Write your MySQL query statement below
- WITH RECURSIVE visit_cnt AS (
- SELECT v.user_id, v.visit_date, COUNT(t.user_id) AS cnt
- FROM Visits v
- LEFT JOIN Transactions t
- ON v.user_id = t.user_id
- AND v.visit_date = t.transaction_date
- GROUP BY v.user_id, v.visit_date
- ),
- nums AS (
- SELECT 0 AS n
- UNION ALL
- SELECT n + 1
- FROM nums
- WHERE n < (SELECT MAX(cnt) FROM visit_cnt)
- )
- SELECT
- nums.n AS transactions_count,
- COUNT(vc.cnt) AS visits_count
- FROM nums
- LEFT JOIN visit_cnt vc
- ON nums.n = vc.cnt
- GROUP BY nums.n
- ORDER BY nums.n;
复制代码 |
|