LeetCode:Database 84.每次访问的交易次数
Posted Xiao Miao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 84.每次访问的交易次数相关的知识,希望对你有一定的参考价值。
要求:写一条 SQL 查询多少客户访问了银行但没有进行任何交易,多少客户访问了银行进行了一次交易等。
结果包含两列:
transactions_count:客户在一次访问中的交易次数
visits_count:在 transactions_count 交易次数下相应的一次访问时的客户数量
transactions_count 的值从 0 到所有用户一次访问中的 max(transactions_count) ,按 transactions_count 排序。
表: Visits的结构
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| visit_date | date |
+---------------+---------+
(user_id, visit_date) 是该表的主键
该表的每行表示 user_id 在 visit_date 访问了银行
表: Transactions的结构
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| user_id | int |
| transaction_date | date |
| amount | int |
+------------------+---------+
该表没有主键,所以可能有重复行
该表的每一行表示 user_id 在 transaction_date 完成了一笔 amount 数额的交易
可以保证用户 (user) 在 transaction_date 访问了银行 (也就是说 Visits 表包含 (user_id, transaction_date) 行)
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 |
| 9 | 2020-01-25 | 66 |
| 8 | 2020-01-28 | 1 |
| 9 | 2020-01-25 | 99 |
+---------+------------------+--------+
Result Table:
+--------------------+--------------+
| transactions_count | visits_count |
+--------------------+--------------+
| 0 | 4 |
| 1 | 5 |
| 2 | 0 |
| 3 | 1 |
+--------------------+--------------+
* 对于 transactions_count = 0, visits 中 (1, "2020-01-01"), (2, "2020-01-02"), (12, "2020-01-01") 和 (19, "2020-01-03") 没有进行交易,所以 visits_count = 4 。
* 对于 transactions_count = 1, visits 中 (2, "2020-01-03"), (7, "2020-01-11"), (8, "2020-01-28"), (1, "2020-01-02") 和 (1, "2020-01-04") 进行了一次交易,所以 visits_count = 5 。
* 对于 transactions_count = 2, 没有客户访问银行进行了两次交易,所以 visits_count = 0 。
* 对于 transactions_count = 3, visits 中 (9, "2020-01-25") 进行了三次交易,所以 visits_count = 1 。
* 对于 transactions_count >= 4, 没有客户访问银行进行了超过3次交易,所以我们停止在 transactions_count = 3 。
SQL语句:
#1.方法1
with c as(
select a.user_id as u1,a.visit_date as v1,b.amount as a1
from visits a
left join transactions b
on a.user_id=b.user_id and a.visit_date=b.transaction_date
)
select e.r1 as transactions_count,count(c1) as visits_count
from(
select count(a1) as c1
from c
group by u1,v1)d
right join (select row_number() over()-1 as r1
from c)e
on e.r1=d.c1
where e.r1<=(select count(a1) as c1
from c
group by u1,v1
order by c1 desc
limit 1)
group by e.r1
order by e.r1 asc;
#2.方法2
with c as(
select count(b.amount)as c1
from visits a
left join transactions b
on a.user_id=b.user_id and a.visit_date=b.transaction_date
group by a.user_id,a.visit_date
)
select
d.c1 as transactions_count,count(c.c1) as visits_count from
(select 0 as c1
union all
select row_number() over() as c1
from transactions
)d
left join
c
on d.c1=c.c1
where d.c1<=(select max(c1) from c)
group by d.c1
order by d.c1 asc
以上是关于LeetCode:Database 84.每次访问的交易次数的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode:Database 69.查询结果的质量和占比