I have a table (audit) like so
id | id_audit | id_booking
--------------------------
8 | 12345 | 10000
7 | 34567 | 10000
6 | 25608 | 10000
5 | 25864 | 10000
4 | 12345 | 10001
3 | 34567 | 10001
2 | 25864 | 10001
1 | 25608 | 10001
I want to return rows, one for each id_booking group but only if id_audit=25608 is newer than id_audit=25864. Newer is a higher id.
So for the records above, it would return only
id | id_audit | id_booking
--------------------------
6 | 25608 | 10000
Is there an efficient query to solve this? The index on the table is id_booking, id_audit
I tried another query which works but it is inefficient and sometimes brings the server to a crawl. This forum will not let me post it due to error:
Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut
2
Answers
I’m not sure if I understood what you meant and since your code isn’t posted, this is my try so let me know if I’m on the right track:
You can do:
Result:
See running example at db<>fiddle.