Current Design
Table: 1_notes
------------------------------------------
| id | text | created_at |
------------------------------------------
| 1_1 | u1 first note | 2018-01-01 10:00:00 |
| 1_2 | u1 second note | 2018-01-03 10:00:00 |
Table: 1_note_timeline
---------------------------------------------------------------------
| note_id | note_created_at | likes_count | created_at |
---------------------------------------------------------------------
| 1_1 | 2018-01-01 10:00:00 | 10 | 2018-01-01 10:00:00 |
| 1_1 | 2018-01-01 10:00:00 | 20 | 2018-01-02 10:00:00 |
| 1_2 | 2018-01-03 10:00:00 | 10 | 2018-01-03 10:00:00 |
| 1_1 | 2018-01-01 10:00:00 | 15 | 2018-01-03 10:00:00 |
Table: 2_notes
--------------------------------------------
| id | text | created_at |
--------------------------------------------
| 2_1 | u2 first note | 2018-01-01 10:00:00 |
| 2_2 | u2 second note | 2018-01-03 10:00:00 |
Table: 2_note_timeline
---------------------------------------------------------------------
| note_id | note_created_at | likes_count | created_at |
---------------------------------------------------------------------
| 2_1 | 2018-01-01 10:00:00 | 10 | 2018-01-01 10:00:00 |
| 2_1 | 2018-01-01 10:00:00 | 20 | 2018-01-02 10:00:00 |
| 2_2 | 2018-01-03 10:00:00 | 10 | 2018-01-03 10:00:00 |
| 2_1 | 2018-01-01 10:00:00 | 15 | 2018-01-03 10:00:00 |
ForEach user there are 2 tables for their notes data.
- {{userId}}_notes table contain the notes of user with id
userId
- {{userId}}_note_timeline table tracks notes likes_count data every day
Required result should have:
- First 2 notes across both users that have most likes
- Likes count must be latest (and NOT MAX_VALUE because notes likes_count can decrease with time)
Final Output
Output
---------------------------------------------------------------
| note_id | note_created_at | likes_count | text |
---------------------------------------------------------------
| 1_1 | 2018-01-01 10:00:00 | 15 | u1 first note |
| 2_1 | 2018-01-01 10:00:00 | 15 | u2 first note |