Data in Log Table
| id | type | created_date |
|---|---|---|
| 1 | in | 2022-01-10 |
| 2 | in | 2022-01-10 |
| 3 | out | 2022-01-10 |
| 4 | out | 2022-01-10 |
| 5 | out | 2022-01-11 |
| 6 | in | 2022-01-12 |
| 7 | in | 2022-01-12 |
| 8 | out | 2022-01-12 |
| 9 | out | 2022-01-12 |
Expected Result
| id | type | created_date |
|---|---|---|
| 6 | in | 2022-01-12 |
| 7 | in | 2022-01-12 |
Query that I am running now:
SELECT * FROM table_log WHERE type = 'in' AND created_date = (SELECT MAX(created_date) FROM table_log WHERE type = 'in')
For instance, I am trying to get data which has type as "in" with latest created_date. My query is working fine but I am wondering if there's anyway I can improve because it running 2 same query.