LeetCode-1336. The number of transactions per visit (difficult)

Table: Visits

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| user_id | int |
| VISIT_DATE | DATE |
+ --------------- + --------- +
(user_id, VISIT_DATE) is the primary key of the table
for each row in the table represents user_id bank access VISIT_DATE
 

表: Transactions

+ --------- + ------------------ +
| Column the Name | Type |
+ ------------- + --------- + -----
| user_id | int |
| on TRANSACTION_DATE | DATE |
| AMOUNT | int |
+ ------------------ + --------- +
the table does not have a primary key, so there may be duplicate rows
for each row in the table represents the user_id transaction_date completed a transaction amount amount
can ensure that users (user) in transaction_date visited Bank ( That table contains Visits (user_id, transaction_date) line)
 

Banks want to get the number of transactions at the bank customer in one visit and the corresponding number of customers in the first visit of the transactions charts

How many customers write a SQL query access to the bank but did not carry out any transactions, how many customers visited a bank transactions, etc.

The results include two:

transactions_count: visit the customer transactions
visits_count: once when the number of customers to access the corresponding transactions in transactions_count
value transactions_count from 0 users to visit all the max (transactions_count) 

Sort by transactions_count

The following are examples of query results format:

Visits 表:
+---------+------------+
| 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 表:
+---------+------------------+--------+
| 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 is |
|. 9 | 2020-01-25 | 66 |
|. 8 | 2020-01 -28 | 1 |
| 9 | 2020-01-25 | 99 |
+ --------- + ------------------ + ---- ---- +
results table:
+ -------------------- + -------------- +
| transactions_count | visits_count |
-------------------- -------------- + + +
| 0 |. 4 |
|. 1 |. 5 |
| 2 | 0 |
| 3 | 1 |
+ -------------------- + -------------- +
* For transactions_count = 0, visits the (1, "2020-01-01"), (2, '2020-01-02'), (12, '2020-01-01') and (19 "2020- 01-03 ") is not traded, so visits_count = 4.
* For transactions_count = 1, visits (for 2, "2020-01-03"), (7, '2020-01-11'), (8, "2020-01-28"), (1, "2020- 01-02 ') and (1, "2020-01-04") conducted a transaction, so visits_count = 5.
* For transactions_count = 2, no customer access to the bank conducted two transactions, so visits_count = 0.
* For transactions_count = 3, visits (9, "2020-01-25") carried out three transactions, so visits_count = 1.
* For transactions_count> = 4, there is no access to the bank clients were more than three times the transaction, so we stopped in transactions_count = 3.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/number-of-transactions-per-visit
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: The first table is to visit the table, record each visit, but not necessarily the transaction, the second table recording transaction information. Query 0 times traded, like the number one.

The first table record one-day visit, but some people in the same transaction several times a day.

Thinking:

Problem solving:

First, for each visit to each user, a total number of transaction (transaction) can be obtained in the following SQL:

SELECT IFNULL(cnt, 0) cnt, COUNT(*) num FROM Visits t1 LEFT JOIN
(SELECT user_id, transaction_date, COUNT(*) cnt FROM Transactions GROUP BY user_id, transaction_date)t2
ON t1.user_id=t2.user_id AND visit_date=transaction_date GROUP BY IFNULL(cnt, 0)
-- 练习
select ifnull(cnt,0) cnt, count(*) num from visits t1 left join 
-- 查询这个id在这个日期下有几次交易。
(select user_id, transaction_date,count(*) cnt from Transactions 
group by user_id,transaction_date)t2
--
on t1.user_id = t2.user_id and visit_date = transaction_date group by ifnull(cnt,0)

Then, it is necessary to generate a table from 0 to a maximum value above this (to N) and its table for JOIN, to obtain the final result. Taking into account the maximum numbers appear, certainly not more than a few rows of tables Transaction, the Transaction open table:

SELECT (@t := @t+1) AS id FROM Transactions t0, (SELECT @t := -1) b

Then let the number is less than equal to the maximum value, the maximum value can be obtained by this SQL:

SELECT MAX(cnt) maxi FROM Visits t11 LEFT JOIN
(SELECT user_id, transaction_date, COUNT(*) cnt FROM Transactions GROUP BY user_id, transaction_date

 Digital generated SQL table becomes:

SELECT (@t := @t+1) AS id FROM Transactions t0, (SELECT @t := -1) b,
(SELECT MAX(cnt) maxi FROM Visits t11 LEFT JOIN
(SELECT user_id, transaction_date, COUNT(*) cnt FROM Transactions GROUP BY user_id, transaction_date)t21
ON t11.user_id=t21.user_id AND visit_date=transaction_date) t31 WHERE @t < maxi

But there is such a problem, if the Transaction is empty, 0 not open this data, so handle it, so that @tfrom the beginning, and then manually UNION a row 0 Results:

SELECT (@t := @t+1) AS id FROM Transactions t0, (SELECT @t := 0) b,
(SELECT MAX(cnt) maxi FROM Visits t11 LEFT JOIN
(SELECT user_id, transaction_date, COUNT(*) cnt FROM Transactions GROUP BY user_id, transaction_date)t21
ON t11.user_id=t21.user_id AND visit_date=transaction_date) t31 WHERE @t < maxi 
 UNION SELECT 0

Then the digital meter and the beginning of the connection can be left in Table 1:

SELECT CEIL(id) transactions_count, IFNULL(num, 0) visits_count FROM
(SELECT (@t := @t+1) AS id FROM Transactions t0, (SELECT @t := 0) b,
(SELECT MAX(cnt) maxi FROM Visits t11 LEFT JOIN
(SELECT user_id, transaction_date, COUNT(*) cnt FROM Transactions GROUP BY user_id, transaction_date)t21
ON t11.user_id=t21.user_id AND visit_date=transaction_date) t31 WHERE @t < maxi 
 UNION SELECT 0) t33
LEFT JOIN
(SELECT IFNULL(cnt, 0) cnt, COUNT(*) num FROM Visits t1 LEFT JOIN
(SELECT user_id, transaction_date, COUNT(*) cnt FROM Transactions GROUP BY user_id, transaction_date)t2
ON t1.user_id=t2.user_id AND visit_date=transaction_date GROUP BY IFNULL(cnt, 0)) t3
ON t33.id=t3.cnt ORDER BY id

 Knowledge Point: This question is more difficult, need to continue in-depth understanding of

Published 144 original articles · won praise 2 · Views 5730

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104789422