I have an order table like this:
|---------------------|------------------|
| created_at | user_id |
|---------------------|------------------|
| 2019-10-14 01:01:59 | 1 |
|---------------------|------------------|
| 2019-10-14 02:01:59 | 2 |
|---------------------|------------------|
| 2019-10-13 00:01:59 | 1 |
|---------------------|------------------|
| 2019-10-14 03:01:59 | 3 |
|---------------------|------------------|
| 2019-10-12 23:01:59 | 2 |
|---------------------|------------------|
| 2019-10-12 23:01:59 | 1 |
|---------------------|------------------|
| 2019-10-10 14:01:59 | 3 |
|---------------------|------------------|
| 2019-10-11 20:01:59 | 2 |
|---------------------|------------------|
| 2019-10-09 12:01:59 | 3 |
|---------------------|------------------|
I grouped my table by this query to get top newest order each user:
select created_at, user_id
from orders
order by user_id, created_at desc
I got these records:
|---------------------|------------------|
| created_at | user_id |
|---------------------|------------------|
| 2019-10-14 01:01:59 | 1 |
|---------------------|------------------|
| 2019-10-13 00:01:59 | 1 |
|---------------------|------------------|
| 2019-10-12 23:01:59 | 1 |
|---------------------|------------------|
| 2019-10-14 02:01:59 | 2 |
|---------------------|------------------|
| 2019-10-12 23:01:59 | 2 |
|---------------------|------------------|
| 2019-10-11 20:01:59 | 2 |
|---------------------|------------------|
| 2019-10-14 03:01:59 | 3 |
|---------------------|------------------|
| 2019-10-10 14:01:59 | 3 |
|---------------------|------------------|
| 2019-10-09 12:01:59 | 3 |
|---------------------|------------------|
Now I want it return only 2 first records each group. How I can do that?
Thank you!