select 姓名,year(日期) 年份
,sum(case when month(日期)=1 then 售票金额-退票金额 else 0 end) as 一月
,sum(case when month(日期)=2 then 售票金额-退票金额 else 0 end) as 二月
,sum(case when month(日期)=3 then 售票金额-退票金额 else 0 end) as 三月
...
,sum(case when month(日期)=12 then 售票金额-退票金额 else 0 end) as 十二月
from 表
group by 姓名,year(日期)
,sum(case when month(日期)=1 then 售票金额-退票金额 else 0 end) as 一月
,sum(case when month(日期)=2 then 售票金额-退票金额 else 0 end) as 二月
,sum(case when month(日期)=3 then 售票金额-退票金额 else 0 end) as 三月
...
,sum(case when month(日期)=12 then 售票金额-退票金额 else 0 end) as 十二月
from 表
group by 姓名,year(日期)
sum(cast month(日期) when 1 then 售票金额- 退票金额 else 0 end) as 一月,
sum(cast month(日期) when 2 then 售票金额- 退票金额 else 0 end) as 二月,
sum(cast month(日期) when 3 then 售票金额- 退票金额 else 0 end) as 三月,
sum(cast month(日期) when 4 then 售票金额- 退票金额 else 0 end) as 四月
...
from table
group by 姓名
sum(cast month(日期) when 1 then 售票金额- 退票金额 else 0 end) as 一月,
sum(cast month(日期) when 2 then 售票金额- 退票金额 else 0 end) as 二月,
sum(cast month(日期) when 3 then 售票金额- 退票金额 else 0 end) as 三月,
sum(cast month(日期) when 4 then 售票金额- 退票金额 else 0 end) as 四月
...
from table
group by 姓名
,sum(case when month(日期)=1 then 售票金额-退票金额 else 0 end) as 一月
,sum(case when month(日期)=2 then 售票金额-退票金额 else 0 end) as 二月
,sum(case when month(日期)=3 then 售票金额-退票金额 else 0 end) as 三月
...
from 表
where year(日期) = 2003
group by 姓名
,sum(case when datepart(month,日期)='1' then 售票金额-退票金额 else 0 end) as 一月
,sum(case when datepart(month,日期)='2' then 售票金额-退票金额 else 0 end) as 二月
,sum(case when datepart(month,日期)='3' then 售票金额-退票金额 else 0 end) as 三月
...
from 表
where datepart(year,日期)= '2003'
group by 姓名
,sum(case when month(日期)=1 then 售票金额-退票金额 else 0 end) as 一月
,sum(case when month(日期)=2 then 售票金额-退票金额 else 0 end) as 二月
,sum(case when month(日期)=3 then 售票金额-退票金额 else 0 end) as 三月
,sum(case when month(日期)=4 then 售票金额-退票金额 else 0 end) as 四月
,sum(case when month(日期)=5 then 售票金额-退票金额 else 0 end) as 五月
,sum(case when month(日期)=6 then 售票金额-退票金额 else 0 end) as 六月
,sum(case when month(日期)=7 then 售票金额-退票金额 else 0 end) as 七月
,sum(case when month(日期)=8 then 售票金额-退票金额 else 0 end) as 八月
,sum(case when month(日期)=9 then 售票金额-退票金额 else 0 end) as 九月
,sum(case when month(日期)=10 then 售票金额-退票金额 else 0 end) as 十月
,sum(case when month(日期)=11 then 售票金额-退票金额 else 0 end) as 十一月
,sum(case when month(日期)=12 then 售票金额-退票金额 else 0 end) as 十二月
from 表
group by 姓名