高手请进:sqlserver性能问题

Fiber 2002-05-09 02:37:51
有这样一个表,表中记载按时间排序的数据,如:
字段:
标志 时间 余额
a 2002-1-1 55.00
b 2002-1-1 76.00
a 2002-1-3 50.00
c 2002-1-1 89.00
a 2002-2-1 43.00
b 2002-1-17 59.00
c 2002-2-10 32.00
现在我需要统计查询出以下数据,即每一行都需要查出其上一行相同标志的余额(按时间)
如需要下列数据:
标志 时间 余额 上一时间 上一余额
a 2002-1-1 55 null null
a 2002-1-3 50 2002-1-1 55
a 2002-2-1 43 2002-1-3 50
b 2002-1-1 76.00 null null
b 2002-1-17 59.00 2002-1-1 76.00
c 2002-1-1 89.00 null null
c 2002-2-10 32.00 2002-1-1 89.00
在考虑到数据量大的情况下,请问如何实现才会保证效率和招待的结果是正确的
...全文
30 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wylyf 2002-05-11
  • 打赏
  • 举报
回复
TO :xyz11111(山楼外)
估计是和楼主是在一起的是吧!
你没有试怎么知道是行不通的?我既然是表就给上了,当然是测试过了。
xyz11111 2002-05-10
  • 打赏
  • 举报
回复
smartdonkey的办法是一个办法,但仍存在一个问题,数据每月增加,而该方法每次都必须处理以行的所有历史数据相关的和不相关的,数据量会很大,可能是几十万条记录,而每月可能只有几千条。而其他人的办法没有上机运行,其实是行不通通的,必须稍加修改。
UN_78 2002-05-09
  • 打赏
  • 举报
回复
这样写是不是太复杂了点,
有没有简单点的?
smartdonkey 2002-05-09
  • 打赏
  • 举报
回复
看看我的解决方法。
drop table table1
go
create table table1(id char(1),dt datetime,amount numeric)
go
insert into table1 values('a','2002-1-1',76)
insert into table1 values('b','2002-1-3',76)
insert into table1 values('a','2002-1-1',50)
insert into table1 values('c','2002-1-1',89)
insert into table1 values('a','2002-2-1',43)
insert into table1 values('b','2002-1-17',59)
insert into table1 values('c','2002-2-10',32)

insert into table1 values('a','2002-3-1',16)
insert into table1 values('b','2002-1-30',66)
insert into table1 values('a','2002-3-17',10)
insert into table1 values('c','2002-2-28',89)
insert into table1 values('a','2002-4-1',73)
insert into table1 values('b','2002-2-17',69)
insert into table1 values('c','2002-3-11',32)
go
create table #tb(rowid int identity(1,1) primary key,id char(1),dt datetime,amount numeric)
go
truncate table #tb

insert into #tb select id,dt,amount from table1 order by id,dt
go
最后。
select t1.id,t1.dt,t1.amount,
case when t2.id is null then t2.dt when t1.id<>t2.id then null else t2.dt end
from #tb t1 left join #tb t2 on t1.rowid=t2.rowid+1

你可以对比以下各种解决方法的效率。
wylyf 2002-05-09
  • 打赏
  • 举报
回复
这样分两部走的话也可以,不过现在写语句养成了个不好的习惯,什么东西都想一个语句搞定,还是斑竹说的是!
Yang_ 2002-05-09
  • 打赏
  • 举报
回复
借用 wylyf(李寻欢) 的表,考虑效率:

select flag,dat,qty,(select max(dat) from #tmp where dat<a.dat and flag=a.flag) as lastdat into #a
from #tmp a

select t.flag as 标志,t.dat as 时间,t.qty as 余额,t.lastdat as 上一时间,
a.qty as 上一余额
from #a t left join #tmp a on t.lastdat=a.dat and t.flag=a,flag



wylyf 2002-05-09
  • 打赏
  • 举报
回复
表为:
create table #tmp
(
flag varchar(10),
dat datetime,
qty int
)
wylyf 2002-05-09
  • 打赏
  • 举报
回复
select flag as 标志,dat as 时间,qty as 余额,(select max(dat) from #tmp where dat<a.dat and flag=a.flag) as 上一时间,
(select qty from #tmp where Flag= a.flag and dat=
(select max(dat) from #tmp where dat<a.dat and flag=a.flag)
) as 上一余额
from #tmp a
weixy 2002-05-09
  • 打赏
  • 举报
回复
只提供思路。至于效率可以建立索引,楼上有好方法吗?
xyz11111 2002-05-09
  • 打赏
  • 举报
回复
weixy():
你理解了fiber的意思,但语句好象有问题,还有更好的办法提高效能吗?
weixy 2002-05-09
  • 打赏
  • 举报
回复
select *, null as pt,null as pm into #t1 from tt order by sign,time

update #t1 pt = max(b.t), pm = b.m from tt b where a.sign = b.sign and a.t < b.t group b.m
Fiber 2002-05-09
  • 打赏
  • 举报
回复
网上没高手吗?
Fiber 2002-05-09
  • 打赏
  • 举报
回复
你误解我的意思了,我需要实现上面的结果
weixy 2002-05-09
  • 打赏
  • 举报
回复
group by 标志 order by 标志,时间

34,590

社区成员

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

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