sql增量行编号

时间:2016-02-25 08:55:37

标签: sql oracle

我正在使用oracle,每次运行query时我都需要增量行编号。我知道通常解决方案是使用rownum但是:

query 1

1  1st row
2  2nd row
3  3rd row

我现在将query按某个值排序,输出应为

1 3rd row
2 1st row
3 2nd row

我怎样才能实现这种行为?

编辑: 我想我的问题不清楚。我不需要使用rownum的解决方案,我需要的是number在我的query的每一行都应该是sequential顺序的query顺序(1,2,3, 4,5)。

所以:

我运行Line nr Some value 1 line 1 2 line 2 3 line 3 4 line 4 ,我的初步结果是

query

现在,我将使用不同的order by

运行相同的Line nr Some value 1 line 4 2 line 3 3 line 2 4 line 1
app.factory('httpRequestInterceptor', function(LoadingService, requestCount) {
    return {
        request: function(config) {
            if (!config.headers.disableLoading) {
                requestCount.increase();
                LoadingService.show();
            }
            return config;
        }
    };
}).factory('httpResponseInterceptor', function(LoadingService, $timeout, error, $q, requestCount) {
    function waitAndHide() {
        $timeout(function() {
            if (requestCount.get() === 0){
                LoadingService.hide();
            }
            else{
                waitAndHide();
            }
        }, 300);
    }

    return {
        response: function(config) {
            requestCount.descrease();
            if (requestCount.get() === 0) {
                waitAndHide();
            }
            return config;
        },
        responseError: function(config) {
            requestCount.descrease();
            if (requestCount.get() === 0) {
                waitAndHide();
            }
            var deferred = $q.defer();
            error.show(config.data, function() {
                deferred.reject(config);
            });
            return deferred.promise;
        }
    };
}).factory('requestCount', function() {
    var count = 0;
    return {
        increase: function() {
            count++;
        },
        descrease: function() {
            if (count === 0) return;
            count--;
        },
        get: function() {
            return count;
        }
    };
})

注意:我对 Line nr

列感兴趣

1 个答案:

答案 0 :(得分:4)

SELECT ROW_NUMBER() OVER ( ORDER BY some_value ) id,
       value
FROM   your_table
ORDER BY some_value

或:

SELECT ROWNUM AS id,
       value
FROM   (
  SELECT value
  FROM   your_table
  ORDER BY some_value
)

示例

CREATE TABLE your_table ( value, some_value, some_other_value ) AS
SELECT 'Line 1', 4, 2 FROM DUAL UNION ALL
SELECT 'Line 2', 3, 4 FROM DUAL UNION ALL
SELECT 'Line 3', 2, 1 FROM DUAL UNION ALL
SELECT 'Line 4', 1, 3 FROM DUAL;

查询1

SELECT ROW_NUMBER() OVER ( ORDER BY some_value ) id,
       value
FROM   your_table
ORDER BY some_value

输出1

        ID VALUE
---------- ------
         1 Line 4 
         2 Line 3 
         3 Line 2 
         4 Line 1 

查询2

SELECT ROWNUM AS id,
       value
FROM   (
  SELECT value
  FROM   your_table
  ORDER BY some_other_value
)

输出2

        ID VALUE
---------- ------
         1 Line 3 
         2 Line 1 
         3 Line 4 
         4 Line 2 

查询3 - 使用DESC订购

(请记住更新两个位置)的排序

SELECT ROW_NUMBER() OVER ( ORDER BY some_other_value DESC ) id,
       value
FROM   your_table
ORDER BY some_other_value DESC; 

输出3

        ID VALUE
---------- ------
         1 Line 2 
         2 Line 4 
         3 Line 1 
         4 Line 3