随机数的准确性

cd_ch 2008-01-24 04:00:14
我的sql得出的结果和条件不同 有没更好的办法

DECLARE @intQuestionCount  INT
SET @intQuestionCount=5 --数量条件

CREATE TABLE #t (id INT,
eCount INT
)
INSERT #t VALUES(1,1)
INSERT #t VALUES(2,2)
INSERT #t VALUES(3,1)
INSERT #t VALUES(4,3)
INSERT #t VALUES(5,1)
INSERT #t VALUES(5,2)
INSERT #t VALUES(6,1)
INSERT #t VALUES(7,4)
INSERT #t VALUES(8,2)
INSERT #t VALUES(9,1)
INSERT #t VALUES(10,1)


declare @tb table(num varchar(500),id int,eCount int)
insert @tb
select newid() as strID,id,eCount from #t
select SUM(eCount) from @tb c where @intQuestionCount>=(select sum(eCount) from @tb t where c.num >=t.num)

DROP TABLE [#t]
...全文
134 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-01-24
  • 打赏
  • 举报
回复
哦,原来是这个意思呀,
换了半天我当是随机从些数据中取出和小这个会下的前几条记录呢

汗一个
cd_ch 2008-01-24
  • 打赏
  • 举报
回复
这样啊,以为有什么好办法。自己判断SUM(eCount)=@intQuestionCount做条件来执行了
fcuandy 2008-01-24
  • 打赏
  • 举报
回复
又是老问题.

要求从 1,5,2,21,55,23,11,....,n 这 m个数中,随机选出一组数,使其 算术和 为 x

这个讨论过很多次了, 只有用穷举才准确,岂能一个随机就能解决?


你的句子:

select SUM(eCount) from @tb c where @intQuestionCount>=(select sum(eCount) from @tb t where c.num >=t.num)

@intQuestionCount 只是对子查询的条件有效, 而非对主查询所列的结果有效. 而子查询中所构成 sum(eCount) 的行与主查询不是完全重合的.

如果sqlserver公布了源码, 你通过你这个句子, 然后通过另外的部分操作sqlserver的内存数据,看看它的子查询匹配了哪些行, 这些行才是你要的.

没有这么简单能实现的.
cd_ch 2008-01-24
  • 打赏
  • 举报
回复
结果是要SUM(eCount)= @intQuestionCount
但是where @intQuestionCount>=(
结果SUM(eCount)<=@intQuestionCount
cd_ch 2008-01-24
  • 打赏
  • 举报
回复
SUM(eCount) 不等于intQuestionCount
-狙击手- 2008-01-24
  • 打赏
  • 举报
回复
DECLARE @intQuestionCount  INT
SET @intQuestionCount=5 --数量条件

CREATE TABLE #t (id INT,
eCount INT
)
INSERT #t VALUES(1,1)
INSERT #t VALUES(2,2)
INSERT #t VALUES(3,1)
INSERT #t VALUES(4,3)
INSERT #t VALUES(5,1)
INSERT #t VALUES(5,2)
INSERT #t VALUES(6,1)
INSERT #t VALUES(7,4)
INSERT #t VALUES(8,2)
INSERT #t VALUES(9,1)
INSERT #t VALUES(10,1)


select identity(int,1,1) as strID,id,eCount into #
from (select top 100 percent* from #t order by newid()) a
select * from #
select SUM(eCount) from # c
where @intQuestionCount>=(select sum(eCount) from # t where c.strID >=t.strID)

DROP TABLE [#t],#
cd_ch 2008-01-24
  • 打赏
  • 举报
回复
是固定随机
cd_ch 2008-01-24
  • 打赏
  • 举报
回复
SET @intQuestionCount=5 --数量条件

/*
-----------
4

(所影响的行数为 1 行)
*/

@intQuestionCount>=(select 这个地方有问题,所以结果和条件不正确
-狙击手- 2008-01-24
  • 打赏
  • 举报
回复
DECLARE @intQuestionCount  INT
SET @intQuestionCount=5 --数量条件

CREATE TABLE #t (id INT,
eCount INT
)
INSERT #t VALUES(1,1)
INSERT #t VALUES(2,2)
INSERT #t VALUES(3,1)
INSERT #t VALUES(4,3)
INSERT #t VALUES(5,1)
INSERT #t VALUES(5,2)
INSERT #t VALUES(6,1)
INSERT #t VALUES(7,4)
INSERT #t VALUES(8,2)
INSERT #t VALUES(9,1)
INSERT #t VALUES(10,1)


select identity(int,1,1) as strID,id,eCount into # from #t
select SUM(eCount) from # c
where @intQuestionCount>=(select sum(eCount) from # t where c.strID >=t.strID)

DROP TABLE [#t],#

/*
-----------
4

(所影响的行数为 1 行)
*/
-狙击手- 2008-01-24
  • 打赏
  • 举报
回复
随机数不是递增随机,
-狙击手- 2008-01-24
  • 打赏
  • 举报
回复
哦,看错了
-狙击手- 2008-01-24
  • 打赏
  • 举报
回复
没看懂,汗

34,587

社区成员

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

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