CREATE OR REPLACE vw_all_data AS
select
tba.reporting_start_date AS start_date,
tba.spent AS spent,
tba.clicks AS clicks,
tba.impressions AS impressions
'tba' AS source
FROM tba_day tba
UNION select
obn.reporting_start_date AS start_date,
obn.spent AS spent,
obn.clicks AS clicks,
tba.impressions AS impressions
'obn' AS source
FROM obn_report obn;
SELECT
DATE_FORMAT(all.reporting_start_date, "%Y-%m") AS 'yearmonth',
MONTH(all.reporting_start_date) AS created_month,
YEAR(all.reporting_start_date) AS created_year,
SUM(all.spend) AS 'spent',
SUM(all.clicks) AS 'clicks',
SUM(all.impressions) AS 'impressions'
FROM vw_all_data all
GROUP BY MONTH(all.reporting_start_date), YEAR(all.reporting_start_date), DATE_FORMAT(all.reporting_start_date, "%Y-%m")