请指教一SQL语句 select a.*, 剩余数量=计划数量-( select sum(实绩数量) from tablename where 订号=a.订号 and 分号=a.分号 and 支号<=a.支号 )from tablename a 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select *,剩余数量=计划数量-(select sum(实绩数量) from t1 where 订号=a.订号子 and 支号<=a.支号)from t1 a --上面的 a.订号子 写错了,应该是: a.订号--测试--测试数据create table t1(订号 varchar(10),分号 int,支号 int,计划数量 int,实绩数量 int)insert t1 select 'WW000001',0,1,100000,20000union all select 'WW000001',0,2,100000,15000union all select 'WW000002',0,1,200000,10000union all select 'WW000001',0,3,100000,30000union all select 'WW000002',0,2,200000,25000go--查询select *,剩余数量=计划数量-(select sum(实绩数量) from t1 where 订号=a.订号 and 支号<=a.支号)from t1 ago--删除测试drop table t1/*--测试结果订号 分号 支号 计划数量 实绩数量 剩余数量 ---------- ----------- ----------- ----------- ----------- ----------- WW000001 0 1 100000 20000 80000WW000001 0 2 100000 15000 65000WW000002 0 1 200000 10000 190000WW000001 0 3 100000 30000 35000WW000002 0 2 200000 25000 165000(所影响的行数为 5 行)--*/ 能写一个SQL语句实现吗? 请问sql2000能否在存储过程里返回一个记录集呢?而不是写到一个表里,然后再去读数据,... 这个SQL如何写 从文本文件中导入数据库~~ 请教Sybase ASE 11.9导出SQL SERVEr2000中乱码问题 这条全文检索命令为什么不行 有一存储过程出错,作用是把一个字符串分开存入一个临时表中。 ado数据库问题 如何直接操纵Excel的数据,是否可以用ADO? 如何根据某个字段是否为空来显示不同的字段 我把“where”当参数传进去,这个存储过程该怎么写? 多条记录变成用分隔符分隔(,)的字符串
select *,剩余数量=计划数量-(select sum(实绩数量) from t1 where 订号=a.订号子 and 支号<=a.支号)
from t1 a
create table t1(订号 varchar(10),分号 int,支号 int,计划数量 int,实绩数量 int)
insert t1 select 'WW000001',0,1,100000,20000
union all select 'WW000001',0,2,100000,15000
union all select 'WW000002',0,1,200000,10000
union all select 'WW000001',0,3,100000,30000
union all select 'WW000002',0,2,200000,25000
go--查询
select *,剩余数量=计划数量-(select sum(实绩数量) from t1 where 订号=a.订号 and 支号<=a.支号)
from t1 a
go--删除测试
drop table t1/*--测试结果订号 分号 支号 计划数量 实绩数量 剩余数量
---------- ----------- ----------- ----------- ----------- -----------
WW000001 0 1 100000 20000 80000
WW000001 0 2 100000 15000 65000
WW000002 0 1 200000 10000 190000
WW000001 0 3 100000 30000 35000
WW000002 0 2 200000 25000 165000(所影响的行数为 5 行)
--*/