关于SQL循环累加的问题

hndxwqt 2007-07-17 07:12:22
有这样的原始表:
X1 X2
-----------------------
2 453
2 120
2 101
2 89
3 111
3 421
3 219
-----------------------
根据X1字段进行累加,我要循环累加后变成下面的表
X1 X2
-----------------------
2 453
2 573
2 674
2 763
3 111
3 532
3 751
-----------------------
好象要用到游标,但不是很会用,没办法就到这里来请教各位朋友了。谢谢。
...全文
1349 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdcorg 2011-05-26
  • 打赏
  • 举报
回复
问题已移到MYSQL区:

http://topic.csdn.net/u/20110525/18/268166de-5df8-4151-a007-c66874b960fd.html
jiangzhong610 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 maco_wang 的回复:]
SQL code

declare @table table (X1 int,X2 int)
insert into @table
select 2,453 union all
select 2,120 union all
select 2,101 union all
select 2,89 union all
select 3,111 union all
select 3,42……
[/Quote] 这段SQL我怎么执行不了啊 是在什么情况下啊 我用的是SQL 2005
cdcorg 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 maco_wang 的回复:]

SQL code

declare @table table (X1 int,X2 int)
insert into @table
select 2,453 union all
select 2,120 union all
select 2,101 union all
select 2,89 union all
select 3,111 union all
select 3,421 union……
[/Quote]

可是这段代码同9楼,MYSQL并不支持 ROW_NUMBER()
引用:http://topic.csdn.net/u/20090504/15/bfbc217b-4ae4-4f36-9810-21de077fb034.html

能否用变量和条件语句的方法,改写下面的代码?

update   # 
set @x2=case when @x1 is null or @x1 <> x1 then x2 else @x2+x2 end,
@x1=x1,
x2=@x2
cdcorg 2011-05-24
  • 打赏
  • 举报
回复
谢谢~

[Quote=引用 2 楼 haiwer 的回复:]

declare @X1 int
declare @X2 int

select * into #
from 原始表
order by x1

update #
set @x2=case when @x1 is null or @x1 <> x1 then x2 else ……
[/Quote]
叶子 2011-05-24
  • 打赏
  • 举报
回复

declare @table table (X1 int,X2 int)
insert into @table
select 2,453 union all
select 2,120 union all
select 2,101 union all
select 2,89 union all
select 3,111 union all
select 3,421 union all
select 3,219

;with maco as
(
select *,row_number() over (partition by X1 ORDER BY X1) AS RID from @table
)
select X1,(select sum(X2) from maco
where X1=t.X1 AND RID<=t.RID) AS X2 from maco t
/*
X1 X2
----------- -----------
2 453
2 573
2 674
2 763
3 111
3 532
3 751
*/
ly745455 2011-05-24
  • 打赏
  • 举报
回复
学习了。
cdcorg 2011-05-24
  • 打赏
  • 举报
回复
问题求解:在MYSQL中,对表的一列数字进行分组循环累加





问题同

http://topic.csdn.net/u/20070717/07/1a2b9317-eec7-453d-8698-044237e47086.html

有这样的原始表:
X1 X2
-----------------------
2 453
2 120
2 101
2 89
3 111
3 421
3 219
-----------------------

根据X1字段进行累加,我要循环累加后变成下面的表
X1 X2
-----------------------
2 453
2 573
2 674
2 763
3 111
3 532
3 751
-----------------------


/////////////////////////////////////////////////
原贴2楼Haiwer的解决方案:


declare @X1 int
declare @X2 int

select * into #
from 原始表
order by x1

update #
set @x2=case when @x1 is null or @x1 <> x1 then x2 else @x2+x2 end,
@x1=x1,
x2=@x2

select * from #

drop table #
/////////////////////////////////////////////////

这段代码适用于MS SQL,但MYSQL中的UPDATE貌似不支持set @变量名=字段名这种写法,求各位帮忙(方法可用游标或不用游标)。






省时省力代码:




CREATE TABLE temptb(
`x1` INT NULL NULL ,
`x2` INT NULL NULL );


INSERT INTO temptb VALUES(2,453);
INSERT INTO temptb VALUES(2,120);
INSERT INTO temptb VALUES(2,101);
INSERT INTO temptb VALUES(2,89);
INSERT INTO temptb VALUES(3,111);
INSERT INTO temptb VALUES(3,421);
INSERT INTO temptb VALUES(3,219);


yibey 2011-05-24
  • 打赏
  • 举报
回复


create table #t (X1 int, X2 int)
insert #t
select 2, 453 union all
select 2, 120 union all
select 2, 101 union all
select 2, 89 union all
select 3, 111 union all
select 3, 421 union all
select 3, 219

with cte as
(
select *,ID=row_number() over(partition by X1 order by X1) from #t
)
select X1,X2=(select sum(X2) from cte a where a.X1=b.X1 AND a.ID<=b.ID) FROM cte b
/*
X1 X2
----------- -----------
2 453
2 573
2 674
2 763
3 111
3 532
3 751

(7 row(s) affected)


hlf1989 2011-05-24
  • 打赏
  • 举报
回复
select   *,id=identity(1,1)   into   #1 
from 原始表

select x1,x2,(select sum(x2) from # a where a.x1=#1.X1 and a.id>=#1.id)
from #1

drop table #1
stswordman 2007-07-23
  • 打赏
  • 举报
回复
sp4 2007-07-17
  • 打赏
  • 举报
回复
不需要游标
hellowork(一两清风) 和 Haiwer(海阔天空) 就很好

Haiwer(海阔天空) 的更完整准确
fa_ge 2007-07-17
  • 打赏
  • 举报
回复
create table t( X1 int, X2 int)
insert t
select 2, 453 union all
select 2, 120 union all
select 2, 101 union all
select 2, 89 union all
select 3,

ALTER TABLE t
ADD id int identity(1,1)

111 union all
select 3, 421 union all
select 3, 219


select X1,X2, X3 =(select SUM(X2) from t where X1=a.X1 AND id>=a.id group by a.X1)
FROM t a
ORDER BY a.X1,a.X2



X1 X2 X3
----------- ----------- -----------
2 89 89
2 101 190
2 120 310
2 453 763
3 111 751
3 219 219
3 421 640

(7 row(s) affected)

迷失的空间 2007-07-17
  • 打赏
  • 举报
回复
这样可以吗?
昵称被占用了 2007-07-17
  • 打赏
  • 举报
回复
declare @X1 int
declare @X2 int

select * into #
from 原始表
order by x1

update #
set @x2=case when @x1 is null or @x1<>x1 then x2 else @x2+x2 end,
@x1=x1,
x2=@x2

select * from #

drop table #
hellowork 2007-07-17
  • 打赏
  • 举报
回复
declare @t table(X1 int, X2 int)
insert @t
select 2, 453 union all
select 2, 120 union all
select 2, 101 union all
select 2, 89 union all
select 3, 111 union all
select 3, 421 union all
select 3, 219

----更新
declare @num int,@x1 int
set @num = 0
update @t set
@num = case when @x1 = X1 then @num + X2 else X2 end,
@x1 = X1,
X2 = @num

----查看
select * from @t

/*结果
X1 X2
-----------------------
2 453
2 573
2 674
2 763
3 111
3 532
3 751
*/
通过慢sql分析的学习,了解什么是慢sql,以及慢SQL会引起那些性能问题。清楚慢sql日志的设置,然后再通过慢sql分析工具的学习,清楚慢sql分析的步骤和流程。慢sql分析工具:mysqldumpslow工具、explain工具、profile工具、Optimizer Trace工具。 提供课程中所使用的sql语句。 课程内容:第一章:课程简介1、课程介绍2、课程大纲 第二章:慢sql简介1、慢sql简介2、慢sql会引起的问题 第三章:慢日志的设置1、慢sql的分析流程2、慢日志参数理解3、慢日志参数设置:第1种方式:my.ini文件设置4、慢日志参数设置:第2种方式:sql脚本设置5、慢日志参数设置-效果验证 第四章:如何发现慢sql1、如何发现慢sql:第1种方式:慢日志文件2、如何发现慢sql:第2种方式:mysql库的slow_log表 第五章:慢sql分析工具1、慢sql提取-mysqldumpslow工具-使用方法2、慢sql提取-mysqldumpslow工具-操作实战3、慢sql的执行计划分析-explain分析-执行计划结果说明4、慢sql的执行计划分析-explain分析-索引介绍+type类型举例5、慢sql的资源开销分析-profile分析-分析步骤6、慢sql的资源开销分析-profile分析-show profile执行阶段说明7、慢sql的资源开销分析-profile分析-完整列表说明+操作实战8、慢sql的跟踪分析-Optimizer Trace分析-分析步骤9、慢sql的跟踪分析-Optimizer Trace表的介绍10、索引失效场景举例 第六章:慢日志清理1、慢日志清理

34,576

社区成员

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

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