题目见:https://leetcode.jp/problemdetail.php?id=1336

Table: Visits+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | int     |
| visit_date    | date    |
+---------------+---------+
(user_id, visit_date) is the primary key 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     |
+------------------+---------+
There is no primary key for this table, it may contain duplicates.
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)

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.

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.

Order the result table by transactions_count.

The query result format is in the following example: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     |
+---------+------------------+--------+
Result table:
+--------------------+--------------+
| transactions_count | visits_count |
+--------------------+--------------+
| 0                  | 4            |
| 1                  | 5            |
| 2                  | 0            |
| 3                  | 1            |
+--------------------+--------------+
* 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

题目很长,简单来说就是给了两张表, 一张visits, 一张Transactions。 一次visit可以有0次或者多次transaction. 现在题目就是要统计transaction 次数在0 ~ max(transaction count)对应的visit次数。
有点绕口。根据例子把中间过程写出来,见下图
第一步先把两张表join起来。这里思考一下用什么join以及key?答案见文末题目1
第二步统计每次visit的transaction
第三步统计每个transaction count对应的visit count
最后就是实现0 ~ max(transactions_count)


/* 1. join visit table and transaction table by user_id and date */
/* 2. count transaction per visit */
/* 3. count users per visit *//* count users per visit */
select id as transaction_count, case when visit_count is null then 0 else 1 end from (select transaction_count, count(user_id) as visit_count
from(/* count transaction per visit */select user_id, visit_date, count(transaction_date) as transaction_countfrom (/* left join visit table and transaction table by user_id and date */select v.user_id, visit_date, transaction_date, amountfrom   visits vleft join transactions ton v.user_id = t.user_id and visit_date = transaction_date) vtgroup by user_id, visit_date) group by visit_count
) t
/* generate id from 0 */
right join (select (@cnt := @cnt + 1) as idfrom transactionscross join (select @cnt := -1)  /*in case transaction table is empty*/union select 0
) tid
on t.transaction_count = tid.idwhere tid.id <= (
/*in case transaction table is empty*/
select 0 as cnt
union
/*get max transaction count*/
select count(1) as cnt
from   transactions
group by user_id, transaction_date
order by cnt desc
limit 1
)
  1. 用什么join 两张表?
    left join, 因为一次visit可以不进行transaction。join的键是user_id, vistie_date。 因为一次visit通过user_id, vistie_date确定。

代码仅供参考,要是跑不通别怪我。我没有钱订阅leetcode premium

参考:
https://code.dennyzhang.com/number-of-transactions-per-visit

leetcode 1336解题思路相关推荐

  1. leetcode 1737 解题思路及注释code 贪心

    备忘录 最近在刷leetcode,好不容易搞懂一道题,还是希望记录下来,以防之后忘记,也希望可以分享给大家思路. 题目要求 给你两个字符串 a 和 b ,二者均由小写字母组成.一步操作中,你可以将 a ...

  2. 路径字符串生成树形结构的思路_资源推荐!顶级程序员必刷宝典!LeetCode中文解题思路重磅问世!...

    今天推荐一个Github资源,该主题是中文LeetCode解题思路,让你完美应对公司笔试! LeetCode简介 LeetCode收录了许多互联网公司的算法题目,被称为刷题神器. 资源内容 第一个部分 ...

  3. 【leetcode】解题思路小结

    链表 ➡️dummy:返回dummy.next,避免处理head被删除情形 ➡️快慢指针 ➡️成环 数组 ➡️哈希表 ➡️用数组做哈希表,下标是key值 ➡️倒序 ➡️双指针 字符串 ➡️堆栈 树 ➡ ...

  4. Leetcode 138. 复制带随机指针的链表 解题思路及C++实现

    解题思路: 主要包括三步. 第一步是遍历一次链表,复制其每一个节点,并将所复制的节点接在其后. 第二步是遍历一次链表,解决拷贝节点的random指针的指向. 第三步是从这个大链表中,拆出原有链表和拷贝 ...

  5. Leetcode 211. 添加与搜索单词 - 数据结构设计 解题思路及C++实现

    解题思路: 用字典树来作为存储的数据结构. 新增单词的时候,就使用字典树插入新单词的方法,与LeetCode 208 题一样. 在查找某一个字典树时,使用深度优先搜索即可. class WordDic ...

  6. Leetcode 106. 从中序与后序遍历序列构造二叉树 解题思路及C++实现

    解题思路: 思路和Leetcode 105题相同.区别在于,在这一题中,后序遍历的最后一个值为根节点. 然后仍然是找到根节点后,划分左右子树,递归构建. /*** Definition for a b ...

  7. Leetcode 313. 超级丑数 解题思路及C++实现

    解题思路: 与Leetcode 264. 丑数 II的解题思路一样,均使用最小堆来存储丑数,第i次更新最小堆时,得到第i大的丑数. 可结合Leetcode 264. 丑数 II的解题思路理解:http ...

  8. LeetCode 309: 一个很清晰的DP解题思路

    问题来源 题目来源链接见下方: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/descript ...

  9. LeetCode 272 Closest Binary Tree Traversal II 解题思路

    原题网址:https://leetcode.com/problems... Given a non-empty binary search tree and a target value, find  ...

  10. 解题思路-LeetCode第713题:乘积小于K的子数组

    解题思路-LeetCode第713题:乘积小于K的子数组 题目描述: 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2 ...

最新文章

  1. IP 网络性能的度量标准
  2. Ubuntu SSH root user cannot login
  3. 【转】TeeChart的用法
  4. python应声虫程序_Python编程基础
  5. 漫步数理统计十二——随机变量的期望
  6. 计算机知识*.jpg,计算机第一篇考试题(基础知识部分)
  7. Python入门--算术运算符,位运算符,比较运算符,布尔运算符,赋值运算符
  8. 自己做量化交易软件(32)小白量化实战6--仿通达信公式选股
  9. Java基础5多线程技术
  10. SystemUI Monkey测试原生代码报错:MLand类ArrayIndexOutOfBoundsException
  11. 华硕 x570 Ryzen 9 5900X电脑 Hackintosh 黑苹果efi引导文件
  12. 鼓励参与计算机考试宣传标语,期末考试励志宣传标语
  13. 功能安全-26262-理论到实践-基础知识-标准机构与认可、认证
  14. 腾讯云添加添加二级域名
  15. 相机标定与矫正(总结)
  16. 判断两个单向链表是否相交
  17. UE4编译grpc,编译打包
  18. 校园论坛网站设计设计与实现
  19. 一篇文章带你了解抖音来客功能的使用方法和注意事项
  20. 手把手教你如何免费注册国际顶级域名

热门文章

  1. 设置mac锁屏但不关闭网络
  2. MS11-003在Internet Explorer中存在允许远程代码执行漏洞复现
  3. 用计算机读取机读卡信息,摄像机SD卡无法读取怎么办
  4. 获得百词斩实体书的单词次序(咸鱼的编程初体验!)
  5. 数据库建表语句的使用及简单实战
  6. 偏差(bias)和方差(variance)区别:
  7. cf1299C-Water Balance
  8. win10计算机用户名修改密码,win10怎么修改administrator账户密码?
  9. 创建数据透视表数据包含合并单元格
  10. word简历排版技巧