这样:
select 年份=convert(varchar(4),year(max(time1)))+'年',
一月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=1),
二月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=2),
三月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=3),
四月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=4),
五月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=5),
六月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=6),
七月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=7),
八月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=8),
九月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=9),
十月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=10),
十一月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=11),
十二月=(select sum(数据) from tablename a where year(a.time1)=year(tablename.time1) and month(a.time1)=12)
FROM tablename
group by year(time1)
以上语句测式通过。可以直接复制使用,如果具体表名,字段名称不同,直接替换表名和列名即可。(表名:TABLENAME,数据字段:数据,时间字段:TIME1)
注意,输入第一个月后,后面的月份复制就行了,然后改一下月份数即可。
之所以使用SUM统计函数,是为了防止一个年月的数据出现多次,如果出现一次,就可以不用这个函数。
温馨提示:答案为网友推荐,仅供参考