I know there are many topics related to this. I tried researching on them, but I couldn't get this right.
I have a following lunch_transaction table:
id  |  user_id  |  date               |  due_amount  |  updated_on
---------------------------------------------------------------------------
1   |      145  | 2018-05-28 12:56:46 |   30.00      | 2018-05-28 12:56:46
2   |      134  | 2018-05-28 13:33:02 |   30.00      | 2018-05-28 13:33:02
3   |      134  | 2018-05-28 13:38:50 |   60.00      | 2018-05-28 13:38:50
4   |      134  | 2018-05-28 13:38:59 |   60.00      | 2018-05-28 13:38:59
5   |      134  | 2018-05-30 13:39:17 |   60.00      | 2018-05-28 13:39:17
6   |      145  | 2018-05-30 15:20:00 |   45.00      | 2018-05-28 15:00:17
I want to get latest date-time record for each user.
Hence desired output should be as :
145  | 2018-05-30 15:20:00 |   45.00      | 2018-05-28 15:00:17
134  | 2018-05-30 13:39:17 |   60.00      | 2018-05-28 13:39:17
But I'm getting the following output:
145  | 2018-05-30 15:20:00 |   30.00      | 2018-05-28 12:56:46
134  | 2018-05-30 13:39:17 |   30.00      | 2018-05-28 13:33:02
Even the column values are not of the same row, i.e. seems like field values in the result-set are mixed from different rows.
Here's my query:
SELECT user_id, MAX(date), due_amount, updated_on 
FROM lunch_transaction 
GROUP BY user_id
What should be the right query to achieve the desired output?
 
     
    