我正在尝试从下面的数据集中安排考试的位置。
,输出数据集如下所示(另一系学生一个接一个)
我无法获得愿望输出。请帮我。我正在使用Oracle 11g速成版。
答案 0 :(得分:2)
使用ROW_NUMBER
分析函数,为每个部门创建排序顺序;然后选择按该数字排序的值。
例如:
SQL> with test (roll_no, name, department) as
2 (select 1, 'anik', 'cse' from dual union all
3 select 2, 'sudipto', 'cse' from dual union all
4 select 3, 'injamam', 'cse' from dual union all
5 select 8, 'sajukta', 'ece' from dual union all
6 select 9, 'gourab', 'ece' from dual union all
7 select 10, 'soumenn', 'ece' from dual),
8 inter as
9 (select roll_no, name, department,
10 row_number() over (partition by department order by roll_no) rn
11 from test
12 )
13 select roll_no, name, department
14 from inter
15 order by rn, department;
ROLL_NO NAME DEP
---------- ------- ---
1 anik cse
8 sajukta ece
2 sudipto cse
9 gourab ece
3 injamam cse
10 soumenn ece
6 rows selected.
SQL>
答案 1 :(得分:2)
您似乎希望它们交错。如果是这样,请在row_number()
中使用order by
:
select s.*
from student s
order by row_number() over (partition by "department" order by "roll_no"),
"department";
Here是SQL Fiddle。
注意:不要将列名用双引号引起来。这意味着标识符的大小写很重要-只会使查询难以编写。