简体   繁体   English

MySql:如何使用两个日期时间字段获取每个组的最新记录?

[英]MySql: How can I get the latest record for each group by using two datetime fields?

I have an order table.我有一个order表。 I want to get the latest record group by order_placed_at, created_at for each user_id .我想group by order_placed_at, created_at为每个user_id获取最新的记录group by order_placed_at, created_at
The first two records have the same order_placed_at but the first record is the latest created_at so I would return record 54447 for user 77.前两条记录具有相同的order_placed_at但第一条记录是最新的created_at因此我将为用户 77 返回记录 54447。
For user 78 the 4th and 5th records have the same order_placed_at but the 4th record has a higher created_at .对于用户 78,第 4 条和第 5 条记录具有相同的order_placed_at但第 4 条记录具有更高的created_at

What query will return my desired results?什么查询会返回我想要的结果? I've been trying different queries but getting nowhere.我一直在尝试不同的查询,但一无所获。

+-------+---------+-----------+----------------------------+----------------------------+
| id    | user_id | amount    | order_placed_at            | created_at                 |
+-------+---------+-----------+----------------------------+----------------------------+
| 54447 |      77 | 488897.20 | 2020-09-10 19:50:00.000000 | 2020-09-11 04:22:27.031544 |
| 54441 |      77 | 488983.00 | 2020-09-10 19:50:00.000000 | 2020-09-11 04:22:26.635398 |
| 53783 |      77 | 500728.50 | 2020-09-10 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53784 |      77 | 500728.50 | 2020-09-09 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53785 |      77 | 500728.50 | 2020-09-09 19:20:00.000000 | 2020-09-11 04:21:10.816455 |
| 53786 |      77 | 500728.50 | 2020-09-08 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53787 |      77 | 500728.50 | 2020-09-08 19:20:00.000000 | 2020-09-11 04:20:10.816455 |
| 53788 |      77 | 500728.50 | 2020-09-07 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53789 |      77 | 500728.50 | 2020-09-07 19:20:00.000000 | 2020-09-11 04:19:10.816455 |
| 53790 |      77 | 500728.50 | 2020-09-06 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53791 |      77 | 500728.50 | 2020-09-06 19:20:00.000000 | 2020-09-11 04:19:10.816455 |
| 53792 |      77 | 500728.50 | 2020-09-06 19:19:00.000000 | 2020-09-11 04:19:10.816455 |
| 54493 |      78 | 489292.05 | 2020-09-10 19:50:00.000000 | 2020-09-11 04:22:24.673776 |
| 54494 |      78 | 489300.87 | 2020-09-10 19:50:00.000000 | 2020-09-11 04:22:24.176158 |
| 54495 |      78 | 489300.87 | 2020-09-09 19:50:00.000000 | 2020-09-11 04:52:24.176158 |
| 54496 |      78 | 489300.87 | 2020-09-09 19:50:00.000000 | 2020-09-11 04:42:20.176158 |
| 54497 |      78 | 489300.87 | 2020-09-08 19:50:00.000000 | 2020-09-11 04:22:24.176158 |
| 54498 |      78 | 489300.87 | 2020-09-08 19:50:00.000000 | 2020-09-11 04:22:20.176158 |
| 54499 |      78 | 489300.87 | 2020-09-07 19:50:00.000000 | 2020-09-11 05:22:24.176158 |
| 54500 |      78 | 489300.87 | 2020-09-07 19:50:00.000000 | 2020-09-11 04:22:20.176158 |
+-------+---------+-----------+----------------------------+----------------------------+

Results:结果:

+-------+---------+-----------+----------------------------+----------------------------+
| id    | user_id | amount    | order_placed_at            | created_at                 |
+-------+---------+-----------+----------------------------+----------------------------+
| 54447 |      77 | 488897.20 | 2020-09-10 19:50:00.000000 | 2020-09-11 04:22:27.031544 |
| 53784 |      77 | 500728.50 | 2020-09-09 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53786 |      77 | 500728.50 | 2020-09-08 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53788 |      77 | 500728.50 | 2020-09-07 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 53790 |      77 | 500728.50 | 2020-09-06 19:20:00.000000 | 2020-09-11 04:21:18.816455 |
| 54493 |      78 | 489292.05 | 2020-09-10 19:50:00.000000 | 2020-09-11 04:22:24.673776 |
| 54495 |      78 | 489300.87 | 2020-09-09 19:50:00.000000 | 2020-09-11 04:42:20.176158 |
| 54497 |      78 | 489300.87 | 2020-09-08 19:50:00.000000 | 2020-09-11 04:22:24.176158 |
| 54499 |      78 | 489300.87 | 2020-09-07 19:50:00.000000 | 2020-09-11 05:22:24.176158 |
+-------+---------+-----------+----------------------------+----------------------------+

EDIT : user 77 has records for days 9/6, 9/7, 9/8, 9/9 and 9/10 so i would want the latest record for each day group by order_placed_at,created_at user 78 has records for days 9/7, 9/8, 9/9 and 9/10 so I would want the latest records for each of those days.编辑用户 77 有第 9/ group by order_placed_at,created_at /8、9/9 和 9/10 天的记录,所以我希望group by order_placed_at,created_at用户 78 有第 9 天/ 7、9/8、9/9 和 9/10,所以我想要那些日子里每一天的最新记录。

WITH cte AS 
( SELECT *, 
         ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY order_placed_at DESC, created_at DESC) rn
         FROM sourcetable 
)
SELECT *
FROM cte
WHERE rn = 1

I updated my question.我更新了我的问题。 – MalcolmInTheCenter – 马尔科姆在中心

WITH cte AS 
( SELECT *, 
         ROW_NUMBER() OVER (PARTITION BY user_id, DATE(order_placed_at) ORDER BY order_placed_at DESC, created_at DESC) rn
         FROM test 
)
SELECT *
FROM cte
WHERE rn = 1
ORDER BY user_id, order_placed_at DESC

fiddle 小提琴

You can use distinct on您可以使用distinct on

select distinct on (t.user_id) t.*
from your_table t
order by t.user_id, t.created_at desc, t.order_placed_at desc

I hope this answer can solved your problem.我希望这个答案可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM