一道经典的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;

这两种方法都可以实现上面的效果,大家谁还有好的方法回复给我,交流一下。

posted @ 2007-12-10 11:26  Forrest Gump  阅读(3914)  评论(4编辑  收藏  举报