注册 登录
编程论坛 SQL Server论坛

如何用sql语句实现数据的累加

ab382717036 发布于 2012-05-11 09:16, 590 次点击
一个表名为stut的表里面的数据是没有分组的
name  type  salary
张三   底薪   2000
李四   底薪   5000
张三   提成   300
李四   提成   100
张三   加班费 200

sql 语句怎么写才能显示下面的输出

name  type  salary count
张三   底薪   2000  2000

张三   提成   300   2300

张三   加班费 200   2500
李四   底薪   5000  5000
李四   提成   100   5100

4 回复
#2
wang5852492012-05-12 23:37
select identity(int,1,1) as id ,name,type,salary into #stut from stut order by name,type

select name,type,salary,(select sum(salary) from #stut s where s.name=s1.name and s.id<=s1.id) as coun from #stut s1
#3
ab3827170362012-05-14 08:24
谢谢
#4
ab3827170362012-05-14 08:26
请问一下s1怎么来的
#5
水木年华_zzu2012-07-12 17:36
Sum(salary )中少了个s
select identity(int,1,1) as id ,name,type,salary into #stut from stut order by name,type

select name,type,salary,(select sum(s.salary) from #stut s where s.name=s1.name and s.id<=s1.id) as coun from #stut s1
1