SQL求往年的工资和
已经知道原表
year salary
------------------ ---------------------
2000 1000
2001 2000
2002 3000
2003 4000
显示查询结果
year salary
------------------ ---------------------
2000 1000
2001 3000
2002 6000
2003 10000
即salary为以前年的工资的和;
我这里提供的答案有两种
第一种:
select b.year,sum(a.salary)
from salary a,salary b
where a.year<=b.year group by b.year
order by b.year;
其中salary 为工资表
第二种:
select s1.year "year",(select sum(s2.salary) from salary s2 where s2.year<=s1.year) "salary" from salary s1;
year salary
------------------ ---------------------
2000 1000
2001 2000
2002 3000
2003 4000
显示查询结果
year salary
------------------ ---------------------
2000 1000
2001 3000
2002 6000
2003 10000
即salary为以前年的工资的和;
我这里提供的答案有两种
第一种:
select b.year,sum(a.salary)
from salary a,salary b
where a.year<=b.year group by b.year
order by b.year;
其中salary 为工资表
第二种:
select s1.year "year",(select sum(s2.salary) from salary s2 where s2.year<=s1.year) "salary" from salary s1;