这就是典型的行转列问题. 首先说下最简单的思路 用union all select year,sum(m1) m1,sum(m2) m2,sum(m3) m3,sum(m4) m4 from ( select year,count m1,0 m2,0 m3,0 m4 from atest where month = 1 union all select year,0 m1,count m2,0 m3,0 m4 from atest where month = 2 union all selec…