This is my main table which contain this type of records
id  | a_id | datetime
------------------------
1   |   1  | 2016-01-07 15:42:14
2   |   1  | 2016-01-08 16:42:14
3   |   1  | 2016-01-09 17:42:14
4   |   2  | 2016-01-07 15:42:14
5   |   2  | 2016-01-08 16:42:14
6   |   2  | 2016-01-09 17:42:14
7   |   2  | 2016-01-10 18:42:14
8   |   2  | 2016-01-11 19:42:14
and I want output like :
id  | a_id | amount
------------------------
3   |   1  | 2016-01-09 17:42:14
8   |   2  | 2016-01-11 19:42:14
How can I get this kind of output?
I tried by this SELECT * FROM (SELECT * FROM table ORDER BY datetime DESC) as tbl GROUP BY a_id
In additionally I want to check that Is there any record inserted in table before Half Hour.
 
     
    