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

MYSQL:obtenga una fila para cada año, con la suma total para cada mes

SELECT
  YEAR(dt) as the_year,
  SUM(CASE WHEN MONTH(dt) = 1 THEN mc_gross ELSE 0 END) AS Total_Jan,
  SUM(CASE WHEN MONTH(dt) = 2 THEN mc_gross ELSE 0 END) AS Total_Feb,
  ...
  SUM(CASE WHEN MONTH(dt) = 12 THEN mc_gross ELSE 0 END) AS Total_Dec
FROM
  transactions
GROUP BY
  the_year;