Sample table 'table':
|table1----|--table2--|--table3---| |----------|----------|-----------| |User------| 5000-----|-15-02-2016| |admin-----| 3500-----|-18-03-2016| |guest-----| 2000-----|-19-03-2016| |guest2----| 200------|-23-03-2016| |guest3----| 2000-----|-24-03-2016| You need to select the date of the first row of the last rows where the sum of the rows on the table2 column is more than 4000. That is, according to the table above at the end should be 19-03-2016