当前位置:网站首页>【LeetCode-SQL】1532. Last three orders
【LeetCode-SQL】1532. Last three orders
2022-06-13 02:06:00 【Python's path to becoming a God】
One 、 subject
surface :Customers
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| name | varchar |
+---------------+---------+
customer_id Is the primary key of the table
This table contains consumer information
surface :Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| customer_id | int |
| cost | int |
+---------------+---------+
order_id Is the primary key of the table
The table contains id by customer_id Customer's order information
Every consumer One order per day
Write a SQL sentence , Find the last three orders of each user . If the user's order is less than 3 pen , Then return all his orders .
The returned results are as follows customer_name Ascending array . If the rankings are the same , Continue to follow customer_id Ascending array . If the ranking is the same , Continue to follow order_date Descending array .
The query result format is shown 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 Yes 4 Orders , Exclude "2020-06-10" The order of , Because it is the oldest order .
Annabelle Only 2 Orders , All return .
Jonathan There happens to be 3 Orders .
Marwan Only 1 Orders .
Result table we follow customer_name Ascending order ,customer_id Ascending order ,order_date Descending order .
Advanced :
- You can write about the recent n A common solution for orders ?
Two 、 solve
1、rank() over()
Ideas :
Code :
select t2.name as customer_name
, t1.customer_id as customer_id
, t1.order_id as order_id
, t1.order_date as order_date
from (
select
customer_id
, order_id
, order_date
, rank() over(partition by customer_id order by order_date desc) as rn
from orders
) t1
join Customers t2 on t1.customer_id = t2.customer_id
where rn <= 3
order by t2.name asc, t1.customer_id asc, t1.order_date desc;
3、 ... and 、 Reference resources
边栏推荐
- Vivo released originos ocean, and the domestic customized system is getting better and better
- 微服务开发环境搭建
- [the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- 反爬虫策略(ip代理、设置随机休眠时间、哔哩哔哩视频信息爬取、真实URL的获取、特殊字符的处理、时间戳的处理、多线程处理)
- Why is "iFLYTEK Super Brain 2030 plan" more worthy of expectation than "pure" virtual human
- Functional translation
- Combining strings and numbers using ssstream
- [learning notes] xr872 GUI littlevgl 8.0 migration (display part)
- How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)
猜你喜欢

Implementation of pointer linked list

记录:如何解决MultipartFile类的transferTo()上传图片报“系统找不到指定的路径“问题【亲测有效】
![How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]](/img/c5/f17333cdb72a1ce09aa54e38dd0a8c.png)
How to solve the problem of obtaining the time through new date() and writing out the difference of 8 hours between the database and the current time [valid through personal test]

Devaxpress Chinese description --tdximageslider (picture rotation control)

LabVIEW大型项目开发提高质量的工具

Gome's ambition of "folding up" app

Sqlserver2008 denied select permission on object'***** '(database'*****', schema'dbo')

Use mediapipe+opencv to make a simple virtual keyboard

dfs与bfs解决宝岛探险

Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
随机推荐
JS get element
Leetcode daily question - 890 Find and replace mode
16 embedded C language interview questions (Classic)
The commercial value of Kwai is being seen by more and more brands and businesses
一、搭建django自动化平台(实现一键执行sql)
[analysis notes] source code analysis of siliconlabs efr32bg22 Bluetooth mesh sensorclient
How does Google's audience work?
C language conditional compilation routine
Huawei equipment configures private IP routing FRR
[learning notes] xr872 GUI littlevgl 8.0 migration (display part)
How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)
Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
蓝牙模块:使用问题集锦
Differences between constants and variables (detailed description) (learning note 3 -- variables and constants)
Mac使用Docker安装Oracle
如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
Establishment of microservice development environment
[unity] problems encountered in packaging webgl project and their solutions
Devexpress implementation flow chart