查询语句!!在线等解决马上给分

wangkenping 2003-10-10 12:03:36
一个表有如下数据
流水号 编码 金额 累计金额
lsh bm je ljje
1 01 100 100
2 02 50 50
3 03 40 40
4 01 50 150
5 06 50 50
6 02 300 350

用一条SQL形成上表的数据样式。(累计金额)
...全文
76 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
--下面是数据测试
declare @表 table(lsh int,bm varchar(2),je int,ljje int)
--流水号', 编码 金额', 累计金额
insert into @表
select 1,'01',100,0
union all select 2,'02',50,0
union all select 3,'03',40,0
union all select 4,'01',50,0
union all select 5,'06',50,0
union all select 6,'02',300,0

select * from @表

update @表 set ljje=(select sum(je) from @表 where bm=a.bm and lsh<=a.lsh)
from @表 a

select * from @表
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
写错字段,应该是:
update 表 set ljje=(select sum(je) from 表 where bm=a.bm and lsh<=a.lsh)
from 表 a
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复

update 表 set ljje=(select sum(ljje) from 表 where bm=a.bm and lsh<=a.lsh)
from 表 a
pengdali 2003-10-10
  • 打赏
  • 举报
回复
注意: tem可以是随便的 它只是个别名如:

select *,(select sum(je) from 表 where bm=a.bm and lsh<=a.lsh) ljje from 表 a

也是对的。
pengdali 2003-10-10
  • 打赏
  • 举报
回复
select *,(select sum(je) from 表 where bm=tem.bm and lsh<=tem.lsh) ljje from 表 tem
leus 2003-10-10
  • 打赏
  • 举报
回复
哈哈,下午回来写
txlicenhe 2003-10-10
  • 打赏
  • 举报
回复
想要什么?
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
测试结果(ljje的累计不对吗?):

lsh bm je ljje
----------- ---- ----------- -----------
1 01 100 100
2 02 50 50
3 03 40 40
4 01 50 150
5 06 50 50
6 02 300 350

(所影响的行数为 6 行)
zjcxc 元老 2003-10-10
  • 打赏
  • 举报
回复
不行,你的是什么数据库?

--下面是我的电脑中的数据测试

--数据测试环境
declare @表 table(lsh int,bm varchar(2),je int,ljje int)
insert into @表
select 1,'01',100,0
union all select 2,'02',50,0
union all select 3,'03',40,0
union all select 4,'01',50,0
union all select 5,'06',50,0
union all select 6,'02',300,0

--更新
update @表 set ljje=(select sum(je) from @表 where bm=a.bm and lsh<=a.lsh)
from @表 a

--显示结果
select * from @表
wangkenping 2003-10-10
  • 打赏
  • 举报
回复
一个表(表名为lkspd)有如下数据
流水号 编码 金额 累计金额
lsh bm je ljje
1 01 100 100
2 02 50 50
3 03 40 40
4 01 50 150
5 06 50 50
6 02 300 350

用一条SQL形成上表的数据样式。(累计金额)
jkljf 2003-10-10
  • 打赏
  • 举报
回复
select *, (select sum(je) from 表 where bm = tem.bm and lsh <= tem.lsh) as ljje from 表 as tem

pengdali是对的
verybigmouthz 2003-10-10
  • 打赏
  • 举报
回复
路过凑凑热闹 :)

pengdali(大力 V3.0) 是对的
pengdali 2003-10-10
  • 打赏
  • 举报
回复
呵呵,除非不是sqlserver
aierong 2003-10-10
  • 打赏
  • 举报
回复
create table 表(lsh int,bm varchar(2),je int)
insert into 表
select 1,'01',100
union all select 2,'02',50
union all select 3,'03',40
union all select 4,'01',50
union all select 5,'06',50
union all select 6,'02',300


select *,
(select sum(je) from 表 where a.bm=bm and a.lsh>=lsh) as 累计金额
from 表 as a



楼主,你说的不行
不明白
yujohny 2003-10-10
  • 打赏
  • 举报
回复
为什么不行,哪里达不到你要求?
wangkenping 2003-10-10
  • 打赏
  • 举报
回复
pengdali(大力 V3.0),zjcxc(邹建) 你们说的不行。
playyuer 2003-10-10
  • 打赏
  • 举报
回复
select *
,(select sum(je) from 表 where bm=a.bm and lsh<=a.lsh) ljje
from 表 a
txlicenhe 2003-10-10
  • 打赏
  • 举报
回复
select *,(select sum(je) from 表 where bm=a.bm and lsh<=a.lsh) ljje from 表 a

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧