CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`exchangeReportSource`
AS SELECT `e`.`dated` AS `dated`,
	cast(sum(`e`.`amountIn`) AS decimal(10, 2)) AS `amountIn`,
	cast(sum(`e`.`rateIn`) AS decimal(10, 4)) AS `rateIn`,
	cast(sum(`e`.`amountOut`) AS decimal(10, 2)) AS `amountOut`,
	cast(sum(`e`.`rateOut`) AS decimal(10, 4)) AS `rateOut`,
	cast(sum(`e`.`amountEntry`) AS decimal(10, 2)) AS `amountEntry`,
	cast(sum(`e`.`rateEntry`) AS decimal(10, 4)) AS `rateEntry`,
	cast(
		IFNULL(`rr`.`value`, `rrc`.`simulatedValue`) AS decimal(10, 4)
	) AS `rateECB`
FROM (
		(
			`vn`.`exchangeReportSourcePrevious` `e`
			LEFT JOIN `vn`.`referenceRate` `rr` ON(`rr`.`dated` = `e`.`dated`)
		)
		JOIN `vn`.`referenceRateConfig` `rrc` ON(1)
	)
GROUP BY `e`.`dated`
ORDER BY `e`.`dated`
