LeetCode MySQL 1532. The Most Recent Three Orders(dense_rank + over窗口函数)

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

Table: Customers

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| name          | varchar |
+---------------+---------+
customer_id is the primary key for this table.
This table contains information about customers.

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| order_date    | date    |
| customer_id   | int     |
| cost          | int     |
+---------------+---------+
order_id is the primary key for this table.
This table contains information about the orders made customer_id.
Each customer has one order per day.

Write an SQL query to find the most recent 3 orders of each user.
If a user ordered less than 3 orders return all of their orders.

Return the result table sorted by customer_name in ascending order and in case of a tie by the customer_id in ascending order. If there still a tie, order them by the order_date in descending order.

The query result format is in the following example:

Customers
+-------------+-----------+
| customer_id | name      |
+-------------+-----------+
| 1           | Winston   |
| 2           | Jonathan  |
| 3           | Annabelle |
| 4           | Marwan    |
| 5           | Khaled    |
+-------------+-----------+Orders
+----------+------------+-------------+------+
| order_id | order_date | customer_id | cost |
+----------+------------+-------------+------+
| 1        | 2020-07-31 | 1           | 30   |
| 2        | 2020-07-30 | 2           | 40   |
| 3        | 2020-07-31 | 3           | 70   |
| 4        | 2020-07-29 | 4           | 100  |
| 5        | 2020-06-10 | 1           | 1010 |
| 6        | 2020-08-01 | 2           | 102  |
| 7        | 2020-08-01 | 3           | 111  |
| 8        | 2020-08-03 | 1           | 99   |
| 9        | 2020-08-07 | 2           | 32   |
| 10       | 2020-07-15 | 1           | 2    |
+----------+------------+-------------+------+Result table:
+---------------+-------------+----------+------------+
| customer_name | customer_id | order_id | order_date |
+---------------+-------------+----------+------------+
| Annabelle     | 3           | 7        | 2020-08-01 |
| Annabelle     | 3           | 3        | 2020-07-31 |
| Jonathan      | 2           | 9        | 2020-08-07 |
| Jonathan      | 2           | 6        | 2020-08-01 |
| Jonathan      | 2           | 2        | 2020-07-30 |
| Marwan        | 4           | 4        | 2020-07-29 |
| Winston       | 1           | 8        | 2020-08-03 |
| Winston       | 1           | 1        | 2020-07-31 |
| Winston       | 1           | 10       | 2020-07-15 |
+---------------+-------------+----------+------------+
Winston has 4 orders, we discard the order of "2020-06-10" 
because it is the oldest order.
Annabelle has only 2 orders, we return them.
Jonathan has exactly 3 orders.
Marwan ordered only one time.
We sort the result table by customer_name in ascending order, by customer_id in ascending order 
and by order_date in descending order in case of a tie.

Follow-up:
Can you write a general solution for the most recent n orders?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/the-most-recent-three-orders
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 窗口函数,计算出排名,选出排名小于等于3的
# Write your MySQL query statement below
select name customer_name, customer_id, order_id, order_date
from
(select customer_id, order_id, order_date, dense_rank() over(partition by customer_id order by order_date desc) rnkfrom Orders
) t
left join Customers
using(customer_id)
where rnk <= 3
order by name, customer_id, order_date desc

694 ms


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474624.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Dota改键

利用全局钩子 制作一个个性化的dota游戏改键&#xff01; dll部分&#xff1a; // FileName: add.cpp#include <Windows.h>/* 定义全局变量 */ HWND g_hwnd NULL; HHOOK g_hKeyboard NULL;// 设置数据段 #pragma data_seg("MySec") static WORD g_keyNum[6]{…

LeetCode MySQL 1501. 可以放心投资的国家

文章目录1. 题目2. 解题1. 题目 表 Person: ------------------------- | Column Name | Type | ------------------------- | id | int | | name | varchar | | phone_number | varchar | ------------------------- id 是该表主键. 该表…

LeetCode MySQL 1270. 向公司CEO汇报工作的所有人

文章目录1. 题目2. 解题1. 题目 员工表&#xff1a;Employees ------------------------ | Column Name | Type | ------------------------ | employee_id | int | | employee_name | varchar | | manager_id | int | ------------------------ employee_…

LeetCode MySQL 570. 至少有5名直接下属的经理

文章目录1. 题目2. 解题1. 题目 Employee 表包含所有员工和他们的经理。 每个员工都有一个 Id&#xff0c;并且还有一列是经理的 Id。 ------------------------------------- |Id |Name |Department |ManagerId | ------------------------------------- |101 |John…

LeetCode MySQL 1132. 报告的记录 II

文章目录1. 题目2. 解题1. 题目 动作表&#xff1a; Actions ------------------------ | Column Name | Type | ------------------------ | user_id | int | | post_id | int | | action_date | date | | action | enum | | extra…

java封装省市区三级json格式,微信开发 使用picker封装省市区三级联动模板

目前学习小程序更多的是看看能否二次封装其它组件&#xff0c;利于以后能快速开发各种小程序应用。目前发现picker的selector模式只有一级下拉&#xff0c;那么我们是否可以通过3个picker来实现三级联动模板的形式来引入其它页面中呢&#xff1f;答案是肯定可以的。那么我的思路…

LeetCode MySQL 1126. 查询活跃业务

文章目录1. 题目2. 解题1. 题目 事件表&#xff1a;Events ------------------------ | Column Name | Type | ------------------------ | business_id | int | | event_type | varchar | | occurences | int | ------------------------ 此表的主键是…

php linux 删除文件夹,linux下如何删除文件夹

linux下删除文件夹的方法&#xff1a;可以使用【rm -rf 目录名】命令进行删除&#xff0c;如【rm -rf /var/log/httpd/access】&#xff0c;表示删除/var/log/httpd/access目录及其下的所有文件、文件夹。直接rm就可以了&#xff0c;不过要加两个参数-rf 即&#xff1a;rm -rf …

LeetCode 1533. Find the Index of the Large Integer(二分查找)

文章目录1. 题目2. 解题1. 题目 We have an integer array arr, where all the integers in arr are equal except for one integer which is larger than the rest of the integers. You will not be given direct access to the array, instead, you will have an API Array…

MySQL Server Architecture

MySQL 服务器架构&#xff1a; 转载于:https://www.cnblogs.com/macleanoracle/archive/2013/03/19/2968212.html

LeetCode MySQL 1479. 周内每天的销售情况(dayname星期几)

文章目录1. 题目2. 解题1. 题目 表&#xff1a;Orders ------------------------ | Column Name | Type | ------------------------ | order_id | int | | customer_id | int | | order_date | date | | item_id | varchar | | quantity …

php的swoole教程,PHP + Swoole2.0 初体验(swoole入门教程)

PHP Swoole2.0 初体验(swoole入门教程)环境&#xff1a;centos7 PHP7.1 swoole2.0准备工作&#xff1a;一、 swoole 扩展安装1 、下载swoolecd/usr/localwget -c https://github.com/swoole/swoole-src/archive/v2.0.8.tar.gztar -zxvf v2.0.8.tar.gzcdswoole-src-2.0.8/2 编…

Git常用命令解说

http://zensheno.blog.51cto.com/2712776/490748 1. Git概念 1.1. Git库中由三部分组成 Git 仓库就是那个.git 目录&#xff0c;其中存放的是我们所提交的文档索引内容&#xff0c;Git 可基于文档索引内容对其所管理的文档进行内容追踪&#xff0c;从而实现文档的版本控…

LeetCode MySQL 1412. 查找成绩处于中游的学生

文章目录1. 题目2. 解题1. 题目 表: Student ------------------------------ | Column Name | Type | ------------------------------ | student_id | int | | student_name | varchar | ------------------------------ student_id 是该表…

LeetCode MySQL 618. 学生地理信息报告(row_number)

文章目录1. 题目2. 解题1. 题目 一所美国大学有来自亚洲、欧洲和美洲的学生&#xff0c;他们的地理信息存放在如下 student 表中。 | name | continent | |--------|-----------| | Jack | America | | Pascal | Europe | | Xi | Asia | | Jane | Americ…

java非必填字段跳过校验,avalon2表单验证,非必填字段在不填写的时候不能通过验证...

avalon2表单验证,非必填字段在不填写的时候不能通过验证代码var vm avalon.define({$id: "validate1",aaa : "",validate: {onError: function(reasons) {reasons.forEach(function(reason) {console.log(reason.getMessage())})},onValidateAll: functio…

jQuery心得5--jQuery深入了解串讲1

1.CSS-DOM 操作 获取和设置元素的样式属性: css()。 获取和设置元素透明度: opacity 属性(css 的一个属性)。 获取和设置元素高度, 宽度: height(), width(). 在设置值时, 若只传递数字, 则默认单位是 px. 如需要使用其他单位则需传递一个字符串, 例如 $(“p:first”).height(“…

LeetCode MySQL 1225. 报告系统状态的连续日期(date_sub + over)

文章目录1. 题目2. 解题1. 题目 Table: Failed ----------------------- | Column Name | Type | ----------------------- | fail_date | date | ----------------------- 该表主键为 fail_date。 该表包含失败任务的天数.Table: Succeeded --------------------…

LeetCode MySQL 1369. 获取最近第二次的活动(over窗口函数)

文章目录1. 题目2. 解题1. 题目 表: UserActivity ------------------------ | Column Name | Type | ------------------------ | username | varchar | | activity | varchar | | startDate | Date | | endDate | Date | -----------------…

侧边导航栏的实现

F:\java\c侧边栏\侧边菜单栏\MySlidingMenu转载于:https://www.cnblogs.com/ct732003684/archive/2013/03/24/2979416.html