sql中关于随机和聚合的问题

来源:百度知道 编辑:UC知道 时间:2024/05/09 16:17:43
tb_Question表
que_ID que_Score que_Detail
1 12.0 aaa
2 22.0 bbb
3 30.0 ccc
4 42.0 ddd
5 2.0 eee
6 1.0 fff
7 3.0 ggg
8 4.0 hhh
9 5.0 iii
10 6.0 jjj
11 7.0 kkk
12 8.0 lll
13 9.0 mmm
14 19.0 nnn
15 39.0 ooo
16 12.0 ppp
17 12.0 qqq

假设这是一张存储了海量题目的表 que_ID:题目编号 que_Score:题目分值 que_Detail:题目内容
现在的需求是:
用这些题目随机生成一张试卷,试卷的总分为100
把需要的 que_ID:题目编号 que_Score:题目分值 que_Detail:题目内容 显示出来。
没有总分啊 随机生成的这套题总分是100
我想要一个SQL Server 的, Oracle的不太会
初学,请多指点

可以实现的,SQL2005测试结果:
创建一个存储过程
create proc suiji
as
declare @temp table
(
id int ,
score int ,
detail varchar(20)
)
declare @allFS int,@id int,@count int
set @allFS=0
set @count=0
while 1=1
begin

--插入题目
set @id=round( rand()*100,0)
if not exists(select * from @temp where id=@id )
begin
select @count=count(*) from tb_Question where que_id=@id
if @count>0
begin
insert @temp
select * from tb_Question where que_id=@id
end
end

--计算分值
select @allFS=sum(score) from @temp
if @allFS=100
break
else if @allFS>100
delete from @temp
end
select * from @temp

select que_ID,que_Score,que_Detail
from (select * from tb_Question
order by dbms_random.random)
where rownum<n;

其中n是你想要随机生成的试题数目。
试试