SQL查询求三年累积数怎么写

select iid ,ye, sum(je) over(partition by ye,iid order by account_id) value from temp
上边就是求的本年累积,我怎么union all 把本年累积和前两年的累积数加在一起算出三年累积数,怎么判断前两年

如果你确认年份是连续的,可以考虑 开窗rows range的方案。
select t2.*,sum(val) over(order by type,val rows between 1 preceding and current row) v from t2;

否则
1、先拿到每年的累计数,比如表 XX
2、再做自关联
select a.累计+b.累计+c.累计
from XXX A
left join XXX B on b.年=a.年-1 and a.标识=b.标识
left join XXX C on C.年=a.年-2 and a.标识=C.标识

注意累计值的 null问题。

select sum(xx) from (xxx union all xxx union all xxx)