CREATE TEMPORARY TABLE tmp.travel (INDEX (id)) ENGINE = MEMORY SELECT t.id, t.ref, t.shipped, t.landed, t.kg, SUM(b.stickers) AS stickers, CAST(SUM(i.density * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as loadedKg, CAST(SUM(167.5 * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as volumeKg FROM travel t LEFT JOIN supplier s ON s.id = t.cargoSupplierFk LEFT JOIN entry e ON e.travelFk = t.id LEFT JOIN buy b ON b.entryFk = e.id LEFT JOIN packaging pkg ON pkg.id = b.packageFk LEFT JOIN item i ON i.id = b.itemFk LEFT JOIN itemType it ON it.id = i.typeFk JOIN warehouse w ON w.id = t.warehouseInFk JOIN warehouse wo ON wo.id = t.warehouseOutFk JOIN country c ON c.id = wo.countryFk LEFT JOIN continent cnt ON cnt.id = c.continentFk JOIN agencyMode am ON am.id = t.agencyFk