sql >> Base de Datos >  >> RDS >> Mysql

SUMA un par de COUNT de dos tablas en función de una variable de tiempo

SELECT `YEAR`, FORMAT(SUM(`count`), 0) AS `Total`
FROM (
    SELECT `Table1`.`YEAR`, COUNT(*) AS `count`
    WHERE `Table1`.`variable` = 'Y'
    GROUP BY `Table1`.`YEAR`
    UNION ALL
    SELECT `Table2`.`YEAR`, COUNT(*) AS `count`
    WHERE `Table2`.`variable` = 'Y'
    GROUP BY `Table2`.`YEAR`
) AS `union`
GROUP BY `YEAR`