分组统计--SQL面试题
本帖最后由 【DBA】小七 于 2009-11-25 14:42 编辑 --author:【DBA】小七 set nocount on declare @t table (年份 int ,工资 int) INSERT INTO @t SELECT 2000,1000 union all select 2001,2000 UNION ALL select 2002,3000 UNION ALL select 2003,4000 SELECT * FROM @t select a.年份, sum(b.工资) as 工资 from @t a,@t b where a.年份>=b.年份 group by a.年份 select 年份, (select SUM(工资) from @t where 年份 <= a.年份) 工资 from @t a --M提供的另外一个写法,呵呵 set nocount off 年份 工资 ----------- ----------- 2000 1000 2001 2000 2002 3000 2003 4000 年份 工资 ----------- ----------- 2000 1000 2001 3000 2002 6000 2003 10000 |