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

时间:2016-03-09 05:06:27

标签: sql oracle

例如,如果是

  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
--------------------------------------------

所以基本上第二个表上的第3列是当然每个学生的索引/序列,但我希望第二个表成为对第二个表的查询(在Oracle中),只是找不到解决方案。我只能赚钱,而不是索引。

2 个答案:

答案 0 :(得分:3)

你正在寻找

ROW_NUMBER()

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

答案 1 :(得分:3)

您需要使用row_number窗口函数。

$('.container').jGravity({
    target: '.someClass',
    ignoreClass: '',
    weight: 25,
    depth: 5,
    drag: true
});