CREATE OR REPLACE DEFINER=`vn`@`localhost` SQL SECURITY DEFINER VIEW `vn`.`exchangeInsuranceEntry` AS SELECT max(`tr`.`landed`) AS `dated`, cast( sum( (`b`.`buyingValue` + `b`.`freightValue`) * `b`.`quantity` ) AS decimal(10, 2) ) AS `Dolares`, cast( sum( (`b`.`buyingValue` + `b`.`freightValue`) * `b`.`quantity` ) / sum( (100 + `e`.`commission`) / 100 * (`b`.`buyingValue` + `b`.`freightValue`) * `b`.`quantity` ) AS decimal(10, 4) ) AS `rate` FROM ( ( `vn`.`entry` `e` JOIN `vn`.`buy` `b` ON(`e`.`id` = `b`.`entryFk`) ) JOIN `vn`.`travel` `tr` ON(`tr`.`id` = `e`.`travelFk`) ) WHERE `tr`.`landed` BETWEEN '2016-01-31' AND `util`.`VN_CURDATE`() AND `e`.`commission` < 0 AND `e`.`currencyFk` = 2 GROUP BY MONTH(`tr`.`landed`), year(`tr`.`landed`)