Skip to content
Advertisement

Select rows with max of duplicates along with distinct data in mysql

I have a scenario in which I need to get the last 3 hours data from a table, by joining it with another table. Please find the 2 tables below

1.PURCHASE TABLE

1.PURCHASE_DETAIL TABLE

The result should be only the one with the latest MESSAGE_STATUS as failure and the table is huge, I just need the last 3 hours data only.

As the result I would like to have the following data (I need the ORDER_NO only)

1.PURCHASE TABLE

The best query I came up with is

But this is not giving me the expected results. Can anyone please have a look and let me know what I am missing. I am still a novice at SQL.

Advertisement

Answer

You can solve this by doing inner join and order by clauses

This will fetch all records within three hours window and fetch the latest record which has the MESSAGE_STATUS as fail

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement