sqlserver,sql语句简化

图片说明

怎么将框里面的语句整合一下,实现行列转化。

用group分组year

有功能,叫透视表,研究一下,应该能解决

行转列问题,参考https://www.jb51.net/article/134326.htm

select year,
case when month=1 then amount end m1,
case when month=2 then amount end m2,
case when month=3 then amount end m3,
case when month=4 then amount end m4
from testtable3 group by year