如何使用oracle sql对连续记录进行排名?

时间:2018-11-17 00:59:49

标签: sql oracle

例如,对于以下数据集:

#include <stdio.h>
#include <stdlib.h>

void fillMatrix();

int main(void){
    int rows, cols;

    printf("\nEnter the number of columns:\n");
        scanf("%d", &cols);
    printf("\nEnter the number of rows:\n");
        scanf("%d", &rows);

    int matrix[rows][cols];


    fillMatrix(&matrix[rows][cols], rows, cols);

    for (int i = 0; i < rows; ++i){
        for (int j = 0; j < (cols - 1); ++j){
            printf("%d ", matrix[i][j]);
        } printf("%d\n", matrix[i][(cols -1)]);
    }
    return 0;
}

void fillMatrix( int *matrix, int rows, int cols ){
    for (int i = 0; i < rows; ++i){
        for (int j = 0; j < cols; ++j){
            printf("\nPlease enter the A(%d,%d) entry:\n", i, j);
                scanf("%d", &*(matrix + (i*cols) + j));
        }
    }
    return;
}

我需要另一列SUBJECT DATE MARKS ======= ===== ===== A 10/01 10 B 10/02 20 B 10/03 30 B 10/04 30 C 10/05 10 C 10/06 20 C 10/07 20 C 10/08 20 ,如下所示。

Level

换句话说,无论是哪种连续组合都在重复,我们必须增加该列的计数器。能做到吗?

1 个答案:

答案 0 :(得分:3)

您正在寻找row_number()

select t.*,
       row_number() over (partition by subject, marks order by date) as level
from t
order by subject, date;