sql怎么写?数量累计合计 AAAA 1 AAAA 2 bbbb 2bbbb 3aaaa 4得到的结果是名称 数量 累计aaaa 1 1aaaa 2 3bbbb 2 2bbbb 2 5aaaa 4 7 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果只有这两个字段,要用临时表如果有id或者其他能为仪表是一条记录的字段:select 名称,数量,(select sum(数量) from tablename where 名称=a.名称 and id<=a.id) as 累计from tablename aorder by id declare @t table(name varchar(10),val int)insert @t select 'aaaa', 1 insert @t select 'aaaa', 2 insert @t select 'bbbb', 2insert @t select 'bbbb', 3insert @t select 'aaaa', 4select id=identity(int,1,1),a.* into gg from @t aselect name,val,sumvalue=(select sum(val) from gg a where a.name=b.name and a.id<=b.id) from gg bdrop table gg-----------------------------------name val sumvalueaaaa 1 1aaaa 2 3bbbb 2 2bbbb 3 5aaaa 4 7 sql语句练习 一个难以解决的问题 熟悉ADO的高手进 求一条SQL 语句,谢谢 求一查询语句 SQL2005通过Opendatasource远程复制access表时提示转换日期错误 向表中填加数据的错误 sqlserver建表的时候怎么设置一个字段初始默认值? SQL问题请教 请问一个简单的问题:如何在sql 2000 的客户端新建数据库? 也许就是你的轻轻一指点,就能让我茅塞顿开啊!我的困惑,在线等! 一条sql语句的 完成一个 报表 import 时 全角字符字段导入发生问题
from tablename a
order by id
insert @t select 'aaaa', 1
insert @t select 'aaaa', 2
insert @t select 'bbbb', 2
insert @t select 'bbbb', 3
insert @t select 'aaaa', 4select id=identity(int,1,1),a.* into gg from @t aselect name,val,sumvalue=(select sum(val) from gg a where a.name=b.name and a.id<=b.id) from gg b
drop table gg-----------------------------------
name val sumvalue
aaaa 1 1
aaaa 2 3
bbbb 2 2
bbbb 3 5
aaaa 4 7