1

我有一张桌子:Questionmaster。它存储 DisciplineId、QuestionId、QuestionText 等...

现在我的问题是:

我需要特定 DisciplineId 的 10 条记录,另一个 DisciplineId 的 20 条记录和 Someother DisciplineId 的 30 条记录……我该怎么办?我怎样才能获得所有声明并只选择 60(10+20+30) 行?

对于一个学科,它的工作方式如下所示:

create or replace function fun_trial(Discipline1,Disc1_NoOfQuestions)
 open cur_out for 
  select getguid() tmp,
  QuestionNo,QuestionText,
  Option1,Option2,
  Option3,Option4,
  Correctanswer,Disciplineid
  from  Questionmaster
  where DisciplineId=discipline1
  AND  rownum <= disc1_NoOfQuestions
   order by tmp ;
return (cur_out);
4

1 回答 1

2

以下查询使用分析函数RANK()对学科内的问题进行排序。然后,外部查询分别为学科 1、2 和 3 选择前十个、前二十个和前三十个问题。

select * from (
  select getguid() tmp
         , QuestionNo
         , QuestionText
         , Option1
         , Option2
         , Option3
         , Option4
         , Correctanswer
         , Disciplineid
         , rank () over (partition by Disciplineid order by QuestionNo ) as rn 
  from  Questionmaster
  where DisciplineId in (1, 2, 3)
)
where ( DisciplineId = 1 and rn <= 10 )
or    ( DisciplineId = 2 and rn <= 20 )
or    ( DisciplineId = 3 and rn <= 30 )
/
于 2010-01-10T05:41:21.060 回答