I have a table in a database:
consumo
pk_consumo fk_ruta fk_vehiculo fecha kms litros litrosx100
-----------------------------------------------------------------------------
1 1 1 2018-04-25 345.000 80
2 2 2 2018-04-26 351.000 70
3 3 1 2018-04-27 360.000 60 x
Each vehicle makes a route and on each route can make a gasoline refueling.
I want to make an average of liters consumed in a route with a SELECT
, but of the vehicle number 1
. In this case we have 3 different routes. I want to make the average consumption on route number 3, where is the x.
How could I do it? I have tried many things but I can not get it out.
The formula I have in mind is the following:
((kms de ruta 3 - kms de ruta 1)/100) / litros de la ruta 3
The result I want is the following:
pk_consumo fk_ruta fk_vehiculo fecha kms litros litrosx100
-----------------------------------------------------------------------------
1 1 1 2018-04-25 345.000 80
2 2 2 2018-04-26 351.000 70
3 3 1 2018-04-27 360.000 60 4,5
I tried the following, but it does not work out:
select (((select sum(kms) from consumo where pk_consumo = 3) - (select sum(kms) from consumo where pk_consumo = (3-1)))/100)/(select sum(litros) from consumo where pk_consumo = 3) total
from consumo
where fk_vehiculo = 2
group by total