当前位置:网站首页>MySQL Interview Questions: Detailed Explanation of User Amount Recharge Interview Questions
MySQL Interview Questions: Detailed Explanation of User Amount Recharge Interview Questions
2022-07-29 23:06:00 【Song Song talks about programming】
Today we continue to organize MySQL interview questions, today's topic is related to user recharge amount

According to the title, let's create a table
create table user_log(login_date date comment 'login date',user_id int comment 'user id');create table payment_log(order_id int primary key auto_increment comment 'order number',pay_date date comment 'payment date',user_id int comment 'user id',revenue int comment 'recharge amount');Next, for a better demonstration, let's fill in the data
insert into user_log values('2022-03-11', 10),('2022-03-19', 10),('2022-03-21', 10),('2022-03-22', 10),('2022-03-25', 10),('2022-03-29', 10),('2022-04-11', 10),('2022-04-13', 10),('2022-04-17', 10),('2022-03-10', 20),('2022-03-11', 20),('2022-03-14', 20),('2022-03-16', 20),('2022-03-20', 20),('2022-04-11', 20),('2022-04-13', 20),('2022-04-17', 20),('2022-04-21', 20),('2022-05-01', 20);insert into payment_log(pay_date, user_id, revenue) values('2022-03-11', 10, 30),('2022-03-11', 10, 30),('2022-03-11', 10, 30),('2022-03-11', 10, 30),('2022-03-19', 10, 50),('2022-03-19', 10, 50),('2022-03-21', 10, 100),('2022-03-21', 10, 100),('2022-03-21', 10, 100),('2022-03-22', 10, 20),('2022-03-22', 10, 20),('2022-03-25', 10, 10),('2022-03-29', 10, 10),('2022-04-11', 10, 70),('2022-04-11', 10, 70),('2022-04-13', 10, 80),('2022-04-17', 10, 90),('2022-03-10', 20, 90),('2022-03-11', 20, 80),('2022-03-14', 20, 60),('2022-03-16', 20, 120),('2022-03-20', 20, 99),('2022-04-11', 20, 87),('2022-04-13', 20, 53),('2022-04-17', 20, 56),('2022-04-21', 20, 77),('2022-05-01', 20, 32),('2022-03-10', 20, 29),('2022-03-11', 20, 30),('2022-03-14', 20, 18),('2022-03-16', 20, 6),('2022-03-20', 20, 19),('2022-04-11', 20, 21),('2022-04-13', 20, 20),('2022-04-17', 20, 76),('2022-04-21', 20, 82),('2022-05-01', 20, 91);Everything is ready, let's complete the requirements
/*Requirement 1: Extract the daily accumulated recharge amount of each userRequirement 2: Extract the recharge amount of each user's last 7-day login*/-- Extract the daily accumulated recharge amount of each user/*Idea: In payment_log, group the amount by user and date*/select user_id, pay_date, sum(revenue) as recharge amountfrom payment_log group by user_id, pay_date;-- Extract the recharge amount of each user's last 7-day login/*To get the corresponding data, we need to connect the queryWe connect the daily reset amount of each user above with the user table*/select t.*, login_date,row_number() over(partition by user_id order by login_date desc) rankingfrom (select user_id, pay_date, sum(revenue) as recharge amountfrom payment_log group by user_id, pay_date) as t join user_logon t.user_id=user_log.user_id and pay_date=login_date;-- Based on this data, find the top 7 and then count the recharge amount of each userselect user_id, sum (recharge amount) from(select t.*, login_date,row_number() over(partition by user_id order by login_date desc) rankingfrom (select user_id, pay_date, sum(revenue) as recharge amountfrom payment_log group by user_id, pay_date) as t join user_logon t.user_id=user_log.user_id and pay_date=login_date) as t1where ranking < 8 group by user_id;-END-
Scan the code to add please note: python, enter the group to communicate face-to-face with Mr. Song: 517745409
边栏推荐
- html+css+php+mysql实现注册+登录+修改密码(附完整代码)
- kaniko --customPlatform参数:支持不同平台的镜像构建(如:arm等)
- 高等数学(第七版)同济大学 习题3-8 个人解答
- JVM 上数据处理语言的竞争:Kotlin, Scala 和 SPL
- Win7x64中使用PowerDesigner连接Oralce数据库报“[Oracle][ODBC][Ora]ORA-12154:TNS:无法解析指定的连接标识符”错误解决方法
- 【企业架构】企业架构框架的新资源出现
- CNCF Keith Chan:分布式云时代,云原生社区的发展与趋势
- C语言实现扫雷(9*9)游戏——详解
- 七、HikariConfig初始化分析
- 通信岗秋招准备
猜你喜欢
随机推荐
B. Party(图论/暴力/顶点的度数)
真offer收割机 第一弹~大厂如何考察候选人?(附答案详解)
【LeetCode】36、有效的数独
嵌入式系统驱动初级【1】——内核模块上_编译方法
二、HikariCP源码分析之获取连接流程二
【面试:并发篇30:多线程:happen-before】
四、HikariCP源码分析之初始化分析一
高性能数据访问中间件 OBProxy(三):问题排查和服务运维
【面试:并发篇33:cas】原子更新器 原子累加器 缓存一致性问题
一级建造师从业者面试需要注意什么问题?
C语言快速入门(为了看源码)
动态规划专题
Embedded system driver primary [1] - kernel module _ compilation method
文档贡献与写作必读-OpenHarmony开发者文档风格指南
推荐 7 个学习 Web3 的开源资源
刀具更换策略问题(完工)
撰写英文文献有哪些技巧?
WSDM‘22推荐系统论文梳理
i2c时序图的详细讲解[通俗易懂]
MySQL数据库进阶篇








![Win7x64中使用PowerDesigner连接Oralce数据库报“[Oracle][ODBC][Ora]ORA-12154:TNS:无法解析指定的连接标识符”错误解决方法](/img/47/9d17f3026b862927af8917f3a8fcad.png)
