 <p>This query will give you all the results you need, you can change date to variables in case you need different dates.</p> <pre><code>SELECT Plate_Number, MAX(kilometer) as max,MIN(kilometer) as min, (max - min) as result, sum(litters) as litters, date FROM log_table WHERE date BETWEEN 5 AND 9 GROUP BY Plate_Number </code></pre> <p><b>EDIT</b></p> <p>In case you always need max and min date, you can change it like</p> <pre><code>WHERE date BETWEEN (SELECT DISTINCT min(date) FROM log_table) AND (SELECT DISTINCT max(date) FROM log_table) </code></pre>