We have "transactions" table:
id amount txn_id status status_text
1 15 123456 0 pending
2 11 123123 0 pending
3 15 123456 1 complete
4 20 321456 0 pending
5 17 987456 0 pending
6 25 321321 0 pending
7 20 321456 1 complete
8 25 321321 1 complete
We need to get the rows with the highest status for each transaction id (txn_id). We are using this query:
SELECT id, amount, txn_id, status, status_text, MAX(status) as max_status
FROM transactions
GROUP BY txn_id
ORDER BY id DESC
We are expecting:
id amount txn_id status status_text max_status
8 25 321321 1 complete 1
7 20 321456 1 complete 1
5 17 987456 0 pending 0
3 15 123456 1 complete 1
2 11 123123 0 pending 0
But we are getting everything right except the "status_text" and the "status" columns which appear to be random. "max_status" is correct.
id amount txn_id status status_text max_status
8 25 321321 1 complete 1
7 20 321456 0 pending 1
5 17 987456 0 pending 0
3 15 123456 0 complete 1
2 11 123123 0 pending 0
I am running out of ideas. How can we get the rows with the highest status for each transaction id?
thank you!