Ver originalSELECT CAST(IF( depositos IS NULL AND retiros IS NULL AND matrize_id IS NULL,'Total', IF(depositos IS NULL AND retiros IS NULL, 'Subtotal', matrize_id)) AS CHAR)`matrize_id`,CAST(IFNULL(`depositos`, '') AS CHAR) depositos,CAST(IFNULL(`retiros`, '') AS CHAR) retiros, `balance`FROM (SELECT `matrize_id`, `depositos`, `retiros`, SUM(`depositos`- `retiros`) balanceFROM `test`.`op_matrizes`GROUP BY `matrize_id`, `depositos`,`retiros` WITH ROLLUP) T1WHERE (depositos IS NULL AND retiros IS NULL) OR (depositos IS NOT NULL AND retiros IS NOT NULL)