如何在oracle中进行排名

时间:2015-03-19 09:06:52

标签: oracle ranking

我有桌子:tb_user。

id|name|value
10|boy|500
20|Ony|200
10|boy|500

当我执行以下查询时:

Select id,name,sum(value) as grant_total from tb_user group by id,name

结果是:

id|name|grant_total
10|boy |1000
20|Ony |200

我想添加1列 - >排名

id|name|grant_total|ranking
10|boy |1000       |1
20|Ony |200        |2

如何进行排名?

3 个答案:

答案 0 :(得分:0)

尝试:

Select id,name,sum(value) as grant_total, rownum as ranking from tb_user group by id,name

答案 1 :(得分:0)

您可以使用 ROWNUM 和子查询来首先排序行:

SQL> WITH DATA AS(
  2  SELECT 10 ID, 'boy' NAME, 1000 grand_total FROM dual UNION ALL
  3  SELECT 20, 'Ony', 200 grand_total from dual
  4  )
  5  SELECT t.*,
  6    rownum ranking
  7  FROM
  8    ( SELECT * FROM DATA ORDER BY grand_total DESC
  9    ) t
 10  /

        ID NAM GRAND_TOTAL    RANKING
---------- --- ----------- ----------
        10 boy        1000          1
        20 Ony         200          2

SQL>

或者,

您可以使用分析功能

例如,

SQL> WITH DATA AS(
  2  SELECT 10 ID, 'boy' NAME, 1000 grand_total FROM dual UNION ALL
  3  SELECT 20, 'Ony', 200 grand_total from dual
  4  )
  5  SELECT t.*, row_number() OVER(ORDER BY grand_total DESC) ranking FROM DATA t;

        ID NAM GRAND_TOTAL    RANKING
---------- --- ----------- ----------
        10 boy        1000          1
        20 Ony         200          2

SQL>

根据要求,您需要使用:

  1. ROW_NUMBER
  2. RANK
  3. DENSE_RANK
  4. 让我们看一个例子,

    SQL> SELECT empno, ename, sal FROM emp;
    
         EMPNO ENAME             SAL
    ---------- ---------- ----------
          7369 SMITH             800
          7499 ALLEN            1600
          7521 WARD             1250
          7566 JONES            2975
          7654 MARTIN           1250
          7698 BLAKE            2850
          7782 CLARK            2450
          7788 SCOTT            3000
          7839 KING             5000
          7844 TURNER           1500
          7876 ADAMS            1100
          7900 JAMES             950
          7902 FORD             3000
          7934 MILLER           1300
    
    14 rows selected.
    
    SQL> SELECT empno,
      2    ename,
      3    sal,
      4    ROW_NUMBER() OVER(ORDER BY sal) rnum,
      5    RANK() OVER(ORDER BY sal DESC) rank,
      6    DENSE_RANK() OVER(ORDER BY sal) drank
      7  FROM emp
      8  ORDER BY empno
      9  /
    
         EMPNO ENAME             SAL       RNUM       RANK      DRANK
    ---------- ---------- ---------- ---------- ---------- ----------
          7369 SMITH             800          1         14          1
          7499 ALLEN            1600          8          7          7
          7521 WARD             1250          4         10          4
          7566 JONES            2975         11          4         10
          7654 MARTIN           1250          5         10          4
          7698 BLAKE            2850         10          5          9
          7782 CLARK            2450          9          6          8
          7788 SCOTT            3000         12          2         11
          7839 KING             5000         14          1         12
          7844 TURNER           1500          7          8          6
          7876 ADAMS            1100          3         12          3
          7900 JAMES             950          2         13          2
          7902 FORD             3000         13          2         11
          7934 MILLER           1300          6          9          5
    
    14 rows selected.
    
    SQL>
    

答案 2 :(得分:0)

最简单的方法是使用ROW_NUMBER()分析函数(或RANK(),如果你想平等地排列关系):

SELECT id, name, SUM(value) AS grand_total
     , ROW_NUMBER() OVER ( ORDER BY SUM(value) DESC ) AS ranking
  FROM tb_user
 GROUP BY id, name
 ORDER BY ranking;