当前位置:网站首页>【每日SQL打卡】DAY 27丨每次访问的交易次数【难度困难-提前放出来】
【每日SQL打卡】DAY 27丨每次访问的交易次数【难度困难-提前放出来】
2022-07-29 11:42:00 【不吃西红柿】
难度困难
SQL架构
表: 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) 行)银行想要得到银行客户在一次访问时的交易次数和相应的在一次访问时该交易次数的客户数量的图表
写一条 SQL 查询多少客户访问了银行但没有进行任何交易,多少客户访问了银行进行了一次交易等等
结果包含两列:
transactions_count:客户在一次访问中的交易次数visits_count:在transactions_count交易次数下相应的一次访问时的客户数量
transactions_count` 的值从 `0` 到所有用户一次访问中的 `max(transactions_count)按 transactions_count 排序
下面是查询结果格式的例子:
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 |
+---------+------------------+--------+
结果表:
+--------------------+--------------+
| 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 。边栏推荐
- 深入理解C# 可空类型
- 大伟 Golang之路
- “祁东黄花菜”是国家地理标志保护产品吗? 蚂蚁新村7月29日答案
- Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
- "100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
- ASN.1接口描述语言详解
- MyCat中间件高可用、读写分离、分片、主从切换、ER分片
- 文件上传漏洞
- 考完PMP后有什么益处
- Basic. Blocking
猜你喜欢

企业微信客户朋友圈一天可以发多少条?都有哪些限制?如何突破朋友圈可展示人数限制?

ECCV 2022 | SSP: 自支持匹配的小样本任务新思想

Self collection online computer wallpaper PHP source code v2.0 adaptive end
![[image processing] image skeleton extraction based on central axis transformation with matlab code](/img/34/80e777c5c0a2a791acd0892e3e0b04.png)
[image processing] image skeleton extraction based on central axis transformation with matlab code

TCP and UDP

2022年企业直播行业发展洞察

【Unity3D】角色控制器(CharacterController)

考完PMP后有什么益处

How to use "copy – link" to accelerate docker to build and optimize cache

Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
随机推荐
Why should kubernetes be used in development environments
golang 实现文件上传下载
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
【Untitled】
如何在匹配行之前使用 grep 显示文件名和行号
2.1冒泡排序(Bubble Sorting)
Is this it?TypeScript actually not difficult!(recommended collection)
7月3日文: 表面上有危险,实属安全周期,大概率会快速上扬的个股
Design and implementation of gbase8s Informix dodker high availability cluster self recovery cluster startup command oninitdb
MarkDown高阶语法手册
【Unity3D】场景切换、退出全屏、退出游戏
socket+websocket
SkiaSharp of WPF custom painting to bounce ball (case)
Learn weekly - 64 - a v2ex style source BBS program
如何对SQuAD1.1数据集进行预处理「详解版」
面试官培训课件(非常实用的企业内训课件)
Mall mall based on flask --- user module
力扣sql刷题(四)
mapbox 地图 生成矢量数据圆
Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life