0

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.

John
  • 109
  • 1
  • 10
  • 1
    *Query that I am running now* The query is optimal enough. Create according index `(type, created_date)` for its improvement simply. *it running 2 same query.* They're not "the same". – Akina Jan 25 '22 at 10:46
  • There are several different approaches to the same problem in the answers to the linked duplicate question. Compare the speed of the various approaches and select the one that works the best for you. – Shadow Jan 25 '22 at 11:12

0 Answers0