以下查询的DENSE-RANK用法

时间:2014-03-28 06:16:19

标签: sql oracle oracle-sqldeveloper

以下是源表:

Order_Id  ====  Job_No  ====  Address  ====   Delivery_Month 
==============================================================
  06543         77771         Newyork         January               
  06543         66662         North NY        February              
  06543         11113         Cupertino       March                 
  06545         22225         Johanseburg     September
  06545         88889         Kingsmead       July
  06540         77773         Sydney          April
  06540         22228         Melbourne       November
  06876         11112         Kandy           August

我希望上表的查询包含以下子句:

1]按Order_ID排序

Order_Id  ====  Job_No  ====  Address  ====   Delivery_Month ==== Rank
========================================================================
  06540         77773         Sydney          April                1
  06540         22228         Melbourne       November             1
  06543         77771         Newyork         January              2
  06543         66662         North NY        February             2
  06543         11113         Cupertino       March                2
  06545         22225         Johanseburg     September            3
  06545         88889         Kingsmead       July                 3    
  06876         11112         Kandy           August               4

这对我有效,在order_id上排名(相同的订单ID应该具有相同的排名)

2]按作业排序应该给出以下输出(基于相同order_id的排名)

Order_Id  ====  Job_No  ====  Address  ====   Delivery_Month ==== Rank
========================================================================
  06876         11112         Kandy           August               1
  06543         11113         Cupertino       March                2
  06545         22225         Johanseburg     September            3
  06540         22228         Melbourne       November             4
  06543         66662         North NY        February             2
  06543         77771         Newyork         January              2
  06540         77773         Sydney          April                4
  06545         88889         Kingsmead       July                 3 

1 个答案:

答案 0 :(得分:3)

select子句中的值不应该打扰你。它们独立于order by子句:

select order_id, job_no, address, delivery_month,
   dense_rank() over (order by order_id) as rank
from table
order by job_no;