Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何基于另一列生成索引列_Sql_Oracle - Fatal编程技术网

Sql 如何基于另一列生成索引列

Sql 如何基于另一列生成索引列,sql,oracle,Sql,Oracle,例如,如果这里是 Student Course --------------------------- student 1 | Math -------------------------- student 1 | English -------------------------- student 1 | Art -------------------------- student 2 | Math --------------

例如,如果这里是

  Student        Course
---------------------------
student 1   |     Math
--------------------------
student 1   |     English
--------------------------
student 1   |     Art
--------------------------
student 2   |     Math
--------------------------
student 2   |     Economics
--------------------------
student 3   |     Math
--------------------------
student 3   |     English
--------------------------
我希望如何根据学生生成课程索引列,如下所示

  Student        Course       Index
--------------------------------------------
student 1   |     Math      |   1
--------------------------------------------
student 1   |     English   |   2
--------------------------------------------
student 1   |     Art       |   3
--------------------------------------------
student 2   |     Math      |   1
--------------------------------------------
student 2   |     Economics |   2
--------------------------------------------
student 3   |     Math      |   1
--------------------------------------------
student 3   |     English   |   2
--------------------------------------------
基本上,第二个表的第三列是每个学生的课程索引/顺序,但我希望第二个表成为Oracle中第二个表的查询,只是找不到解决方案。我只能算出总数,但不能算出索引。

就是你要找的

SELECT Student
       ,Course
       ,ROW_NUMBER() OVER (PARTITION BY Student ORDER BY Course DESC) AS "Index"
FROM your_table;
这就是你要找的

SELECT Student
       ,Course
       ,ROW_NUMBER() OVER (PARTITION BY Student ORDER BY Course DESC) AS "Index"
FROM your_table;

您需要使用行数窗口功能

select student, course, 
    Row_number() over (partition by student order by course) 
from the_table

您需要使用行数窗口功能

select student, course, 
    Row_number() over (partition by student order by course) 
from the_table

好吧,你需要引用索引,因为它是一个关键字。True@JoachimIsaksson。编辑,你被抢了。甚至连文档的链接都没有。没有正义-好吧,你需要引用索引,因为它是一个关键字。True@JoachimIsaksson。编辑,你被抢了。甚至连文档的链接都没有。没有正义-每个学生的课程顺序重要吗?每个学生的课程顺序重要吗?