你有更好的办法吗? 讨论一下!

simonhehe 2008-07-11 02:20:37
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90

--1 初次操作得到如下结果(当前记录的AccValue=自身length + 前一记录的AccValue)
id length AccValue
----------- ----------- -----------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
--2 当增加新记录时AccValue如果计算最合理有效
--3 当改动某条记录的length 后,受到影响的记录的AccValue如何计算最合理有效
...全文
196 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzuhui 2008-07-11
  • 打赏
  • 举报
回复
看不懂!
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wgzaaa 的回复:]
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
declare @c int,@c2 int
select @c=0
update @t set @c=@c+length,AccValue=@c
select * from @t
------------------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
我认为这样更好
[/Quote]
这种做法的确不错
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 liangpei2008 的回复:]
如果记录是用户输入,而用户每次都要看一下最新的更新结果,大家也要用什么相关子查询,或游标,或用TSQL模拟游标来做??
如果数据在上10万条呢?

SQL code
所以该问题的核心在于如何不重复累计已统计的信息。
下面是个人观点:
为已参与统计的数据加上处理标识。如果在10000中加上一条记录,那直接就以最后的值+新增记录。
如果在已有的记录中做修改,则直接在累计字段+增量。
如果不这样处理,再好的SQL语句,再…
[/Quote]

新增或修改记录时使用增量进行批量更新,我也是这么干

对问题1有什么好的解决办法

希望数据量在 <1W <10W <100W >100W 这几个区间时处理效率没有太明显差别
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wgzaaa 的回复:]
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
declare @c int,@c2 int
select @c=0
update @t set @c=@c+length,AccValue=@c
select * from @t
------------------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
我认为这样更好
[/Quote]

模拟游标比子查询或循环要好 :D
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chuifengde 的回复:]
SQL codecreate function getts(@id int)
returns bigint
as
begin
declare @ss bigint
select @ss=isnull(sum(length),0) from tsst where id<=@id
return @ss
end
……
[/Quote]

使用这样的子查询在大数据量的情况下,只会越来越糟
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hery2002 的回复:]
你想讨论什么?
一种是循环方式,
一种是子查询方式,
[/Quote]

^_^

针对这样的业务要求,在大数据量的情况下,用什么样的方法实现效率更高?
可以写下自己的实现方法,并说明优缺点
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
如果记录是用户输入,而用户每次都要看一下最新的更新结果,大家也要用什么相关子查询,或游标,或用TSQL模拟游标来做??
如果数据在上10万条呢?

所以该问题的核心在于如何不重复累计已统计的信息。
下面是个人观点:
为已参与统计的数据加上处理标识。如果在10000中加上一条记录,那直接就以最后的值+新增记录。
如果在已有的记录中做修改,则直接在累计字段+增量。
如果不这样处理,再好的SQL语句,再优秀的表设计也枉然.

lff642 2008-07-11
  • 打赏
  • 举报
回复
这个问题有趣.
wgzaaa 2008-07-11
  • 打赏
  • 举报
回复
对更新,我认为没有什么好说的,不过不使用触发器,要求前台直接使用触发器中的语句在效率方面有优势,但不便于数据管理
wwd252 2008-07-11
  • 打赏
  • 举报
回复
楼上的不错
wgzaaa 2008-07-11
  • 打赏
  • 举报
回复
第二种方法好点,但也和第一种方法一样重复扫描或反复执行
wgzaaa 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
declare @c int,@c2 int
select @c=0
update @t set @c=@c+length,AccValue=@c
select * from @t
------------------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
我认为这样更好
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int IDENTITY(1,1),length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
1,更新方法
......
2 当增加新记录时AccValue如果计算最合理有效 ,
先得到最后一个 AccValue ,然后插入数据
  declare @last_AccValue int
set @last_AccValue=(select top 1 AccValue from @t order by id desc)
insert into @t(length,AccValue) values(@length,@length+@last_AccValue)


3、当改动某条记录的length 后,受到影响的记录的AccValue如何计算最合理有效
个人认为,这个时候,只有先更新一条记录,然后再用方法一,对ID>=更新ID 的记录全部进行重新计算

chuifengde 2008-07-11
  • 打赏
  • 举报
回复
create  function getts(@id int)
returns bigint
as
begin
declare @ss bigint
select @ss=isnull(sum(length),0) from tsst where id<=@id
return @ss
end
go
create table tsst(id int,length int, AccValue as dbo.getts(id))
insert tsst(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
chuifengde 2008-07-11
  • 打赏
  • 举报
回复
用计算列最方便了
hery2002 2008-07-11
  • 打赏
  • 举报
回复
你想讨论什么?
一种是循环方式,
一种是子查询方式,
viptiger 2008-07-11
  • 打赏
  • 举报
回复
=.=
hery2002 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int,length int, AccValue int) 
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90

select id,
length,
(select sum(length)
from @t b
where b.id <= a.id) AccValue
from @t a
/*
id length AccValue
----------- ----------- -----------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
*/
utpcb 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90

select id,
length,
(select sum(length)
from @t b
where b.id <= a.id) AccValue
from @t a


1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
simonhehe 2008-07-11
  • 打赏
  • 举报
回复

不希望只是简单实现,要说出具体的优劣
加载更多回复(3)
你在学习Python数据分析的时候,是否遇到过在这些问题? 别慌!这些都是数据科学入门常见问题。从入门到上手再到解决实际问题,数据科学看似复杂,但如果你掌握了正确的学习方法,完全可以极速入门。 【职场人进阶必备  数据分析/挖掘一点通】 如今的职场上,90%以上的岗位都会涉及数据问题。 以产品文案岗位为例,在一个新产品推向市场之前,文案需要考虑: 此时,可以关注一下市场上已有的相关产品推广数据,如:哪些文案打开率更高?哪些文案转化更好?目标用户的购买习惯如何? 以此作为下一步工作开展的依据,对产品文案工作者来说,可以少走很多弯路。 学会数据分析/挖掘,等于站巨人的肩膀上工作,轻松且高效。 【爬虫、数据分析、数据挖掘知识点三合一】数据问题一网打尽 本课程将知识点悉数融入实战项目,不空谈语法,帮助学员在实践中获取知识,目标是:让学员能自主完成数据采集、数据分析与数据挖掘。 学习完本课程,你可以熟练掌握: 【实战案例超实用,轻松拥有“睡后收入”!】 本课程以股票案例为主线,串联爬虫、数据分析以及数据挖掘多个知识点。 通过实战案例演练,你可以全面掌握股票收益的分析和预判方法,在收获新技能的同时,也有机会获得“睡后收入”! 四大优势: 三重权益:

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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