运行时间要求在1秒种内完成

lishengyu 2005-12-30 05:16:30
有一个表如下 

CREATE TABLE [#tmp](
px int IDENTITY(1,1),
value int
)
大约有10000条记录,value 字段是一个随机的值,
现要求是输入的一个值M,要找出前N条记录,
且N条记录value的总和刚好不大M(意思是再多一条记录都会大于M)。
语句运行时间要求在1秒种内完成
说明:
 px值小的认为是排在px大的前面
  主要想考的是能否在1秒种内完成
 可以用存领储过程、函数 
比如:[#tmp] 为
px , value
5 ,5
4 ,12
7 ,9
1 ,3
3 ,6
6 ,23
2 ,15

录入 20 找出
px ,value
1 ,3
2 ,15
...全文
154 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2005-12-31
  • 打赏
  • 举报
回复
http://blog.csdn.net/libin_ftsafe/archive/2005/12/31/566908.aspx
子陌红尘 2005-12-31
  • 打赏
  • 举报
回复
楼上的方法是错了,楼主还是用游标吧。
iwl 2005-12-30
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘) 的方法已经可以了
子陌红尘 2005-12-30
  • 打赏
  • 举报
回复
--试试这个,不需要用游标
create table #tmp(px int,value int)
insert into #tmp select 1,3
insert into #tmp select 7,9
insert into #tmp select 3,6
insert into #tmp select 5,5
insert into #tmp select 6,23
insert into #tmp select 2,15
insert into #tmp select 4,12

declare @px int,@s int,@m int
set @s=0
set @m=20

select
@px=case when @s<=@m then px else @px end,
@s =@s+case when @s<=@m then value else 0 end
from
#tmp
order by px

select * from #tmp where px<@px order by px

/*
px value
----------- -----------
1 3
2 15
*/

drop table #tmp
lishengyu 2005-12-30
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘) 先前的方法非常快
能不能想个办法解决 px按从小到大顺序的问题呀

呵呵,是呀游标也挺快的
子陌红尘 2005-12-30
  • 打赏
  • 举报
回复
游标真的很慢吗?楼主在100,000数据的环境下试试,已经算很快了。
pbsql 2005-12-30
  • 打赏
  • 举报
回复
这种情况游标快些当然选择游标了,为什么非不用游标呢?
lishengyu 2005-12-30
  • 打赏
  • 举报
回复
人家不是说游标很慢吗,为什么非得用游标呢
就没有别的办法了吗
-狙击手- 2005-12-30
  • 打赏
  • 举报
回复
create table #tmp(px int,value int)
go
insert into #tmp select 5,5
insert into #tmp select 4,12
insert into #tmp select 7,9
insert into #tmp select 1,3
insert into #tmp select 3,6
insert into #tmp select 6,23
insert into #tmp select 2,15

select px,
(select sum(isnull(value,0)) from #tmp where px <= a.px )as v into #tmp1 from #tmp a order by px
select * from #tmp1
declare @m int,@n int
set @m=20
select * from #tmp1 where v <= @m
set @n = @@rowcount
print @n
SET ROWCOUNT @n

select * from #tmp order by px
set rowcount 0

drop table #tmp

drop table #tmp1

/*

px v
----------- -----------
1 3
2 18

(所影响的行数为 2 行)

2
px value
----------- -----------
1 3
2 15

(所影响的行数为 2 行)
*/
子陌红尘 2005-12-30
  • 打赏
  • 举报
回复
create table #tmp(px int,value int)
insert into #tmp select 5,5
insert into #tmp select 4,12
insert into #tmp select 7,9
insert into #tmp select 1,3
insert into #tmp select 3,6
insert into #tmp select 6,23
insert into #tmp select 2,15

declare @px int,@value int,@sum int,@m int,@px1 int
set @sum=0
set @m=20

declare t_cursor cursor for select px,value from #tmp order by px

open t_cursor
fetch next from t_cursor into @px,@value

while @@fetch_status=0
begin
if(@sum>@m)
break
set @sum=@sum+@value
set @px1=@px
fetch next from t_cursor into @px,@value
end
close t_cursor
deallocate t_cursor

select * from #tmp where px<@px1 order by px
/*
px value
----------- -----------
1 3
2 15
*/

drop table #tmp
lw1a2 2005-12-30
  • 打赏
  • 举报
回复
求出的px都是顺次的?
lishengyu 2005-12-30
  • 打赏
  • 举报
回复
up

34,575

社区成员

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

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