Hello I have this table: (id, carId, gasStationId, liters, totalPrice). I would like to create query for sum total costs in each gas stations by car. I know how to sum total costs by gas station, but I don't know how to group by car. Here is my query:
select sum(totalPrice) as totalCosts
, count(*) as countPurchases
, g.name
, p.carId
from purchase as p
join gas_station as g
on g.id = p.id
group
by gasStationId
I would like get this result:
┌─────────────┬──────┬──────┐ │ GasStation1 │ Car1 │ 1000 │ ├─────────────┼──────┼──────┤ │ GasStation1 │ Car2 │ 1500 │ │ GasStation2 │ Car2 │ 500 │ │ GasStation2 │ Car1 │ 700 │ └─────────────┴──────┴──────┘
group by gasStationId, carIdor are you wanting to join another table you're not showing?gasStationIdin your grouping? It looks like it should've beeng.id. But I following your query in my answer.