当前位置:网站首页>【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
边栏推荐
- C language volatile learning
- Devexpress implementation flow chart
- Installing Oracle with docker for Mac
- Using atexit to realize automatic destruct of singleton mode
- [programming idea] communication interface of data transmission and decoupling design of communication protocol
- Stm32+ze-08 formaldehyde sensor tutorial
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- Establishment of microservice development environment
- Interruption of 51 single chip microcomputer learning notes (external interruption, timer interruption, interrupt nesting)
- Delphi 10.4.2 release instructions and installation methods of three patches
猜你喜欢

Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)

Simple ranging using Arduino and ultrasonic sensors

Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86

Review the history of various versions of ITIL, and find the key points for the development of enterprise operation and maintenance

What did Hello travel do right for 500million users in five years?

Stm32+ze-08 formaldehyde sensor tutorial

华为设备配置CE双归属

The scientific innovation board successfully held the meeting, and the IPO of Kuangshi technology ushered in the dawn

Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient

Interruption of 51 single chip microcomputer learning notes (external interruption, timer interruption, interrupt nesting)
随机推荐
Parameter measurement method of brushless motor
SQL server deletes all tables and all stored procedures in the database
C language volatile learning
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
[sequence structure, branch structure, loop structure, continue statement, break statement, return statement] (learning Note 6 -- C language process control)
华为设备配置双反射器优化虚拟专用网骨干层
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
When AI meets music, iFLYTEK music leads the industry reform with technology
Configuring virtual private network FRR for Huawei equipment
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
Implementation of pointer linked list
JS get element
uniapp 预览功能
Ruixing coffee moves towards "national consumption"
Magics 23.0 how to activate and use the slice preview function of the view tool page
SWD debugging mode of stm32
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
VI keyboard diagram
What is Google plus large text ads? How to use it?
Gome's ambition of "folding up" app