SELECT iot.vat, pgc.name, IF(pe.equFk IS NULL, taxableBase, 0) AS base, pgc.rate / 100 AS vatPercent FROM invoiceOutTax iot JOIN pgc ON pgc.code = iot.pgcFk LEFT JOIN pgcEqu pe ON pe.equFk = pgc.code JOIN invoiceOut io ON io.id = iot.invoiceOutFk WHERE io.ref = ? ORDER BY iot.id