You are given two tables: Visits and Transactions.
The Visits table records every visit a user makes to the bank with columns user_id and visit_date.
The Transactions table records every transaction with columns user_id, transaction_date, and amount. Multiple transactions can occur on the same date.
A bank wants to create a chart showing:
- How many users made exactly 0 transactions in a single visit
- How many users made exactly 1 transaction in a single visit
- How many users made exactly 2 transactions in a single visit
- And so on...
Write a SQL query to count the number of users for each possible transaction count (from 0 to the maximum transactions made by any user in a single visit).
Return the result with columns transactions_count and visits_count, ordered by transactions_count.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | ID of the user visiting the bank |
visit_date
PK
|
date | Date of the visit |
| Column Name | Type | Description |
|---|---|---|
user_id
|
int | ID of the user making transaction |
transaction_date
|
date | Date of the transaction (same as visit_date) |
amount
|
int | Transaction amount |
Input & Output
| user_id | visit_date |
|---|---|
| 1 | 2020-01-01 |
| 2 | 2020-01-02 |
| 12 | 2020-01-01 |
| 19 | 2020-01-02 |
| 1 | 2020-01-03 |
| 2 | 2020-01-03 |
| 2 | 2020-01-04 |
| 3 | 2020-01-05 |
| user_id | transaction_date | amount |
|---|---|---|
| 1 | 2020-01-02 | 120 |
| 2 | 2020-01-03 | 22 |
| 3 | 2020-01-05 | 101 |
| 12 | 2020-01-01 | 30 |
| 1 | 2020-01-03 | 38 |
| 1 | 2020-01-03 | 12 |
| transactions_count | visits_count |
|---|---|
| 0 | 4 |
| 1 | 3 |
| 2 | 1 |
Breaking down each visit: User 1 on 2020-01-01 (0 transactions), User 2 on 2020-01-02 (0 transactions), User 12 on 2020-01-01 (1 transaction), User 19 on 2020-01-02 (0 transactions), User 1 on 2020-01-03 (2 transactions), User 2 on 2020-01-03 (1 transaction), User 2 on 2020-01-04 (0 transactions), User 3 on 2020-01-05 (1 transaction). So we have 4 visits with 0 transactions, 3 visits with 1 transaction, and 1 visit with 2 transactions.
| user_id | visit_date |
|---|---|
| 1 | 2020-01-01 |
| 2 | 2020-01-02 |
| user_id | transaction_date | amount |
|---|
| transactions_count | visits_count |
|---|---|
| 0 | 2 |
Both visits have no corresponding transactions, so all visits have 0 transactions. The result shows 2 visits with 0 transactions.
| user_id | visit_date |
|---|---|
| 1 | 2020-01-01 |
| user_id | transaction_date | amount |
|---|---|---|
| 1 | 2020-01-01 | 100 |
| 1 | 2020-01-01 | 200 |
| 1 | 2020-01-01 | 300 |
| transactions_count | visits_count |
|---|---|
| 0 | 0 |
| 1 | 0 |
| 2 | 0 |
| 3 | 1 |
User 1's single visit on 2020-01-01 has 3 transactions. The result must show the complete range from 0 to 3, where only transactions_count = 3 has 1 visit.
Constraints
-
1 ≤ user_id ≤ 1000 -
visit_dateandtransaction_dateare valid dates -
1 ≤ amount ≤ 1000 - Each transaction corresponds to a visit (guaranteed by problem statement)