/*
create table 表1(日期 datetime,收益 numeric(10,2))
go
insert into 表1(日期,收益) values('2022-1-12',10.1)
insert into 表1(日期,收益) values('2022-2-28',20.2)
insert into 表1(日期,收益) values('2023-3-28',30.3)
insert into 表1(日期,收益) values('2023-4-06',40.4)
insert into 表1(日期,收益) values('2024-5-06',50.5)
*/
-- 1.先按年份汇总看看
select year(日期)as 年份,SUM(收益) as 收益 from 表1 group by year(日期)
-- 2.你要的查询结果
select t1.年份,SUM(t2.收益) as 收益 from
(select YEAR(日期) as 年份 from 表1 group by YEAR(日期))t1,
(select year(日期)as 年份,SUM(收益) as 收益 from 表1 group by year(日期))t2
where t1.年份 >= t2.年份
group by t1.年份