[英]rank() and over () in oracle queries
我需要了解這兩個Oracle查詢之間的區別,特別是rank() over(order by length(cgp.group_name) desc)
和rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc) ranking
。
我在Google上搜索了有關排名的信息,並在下面進行了了解:
rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc) ranking
:根據group_name的最大匹配,結果中將顯示一個排名列,其值為1,2,3。
select csc.subscriber_num csc.group_id,
rank() over(order by length(cgp.group_name) desc) ranking
from scallforward_info csc ,
gprofile cgp
where '0120111' like csc.subscriber_num||'%'
and GROUP_NAME like 'TEST' ||'%'
and csc.account_number=99995555
and csc.group_id= cgp.group_id
and csc.ver = 1
and cgp.ver = 1;
select csc.subscriber_num csc.group_id,
rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc) ranking
from scallforward_info csc ,
gprofile cgp
where '0120111' like csc.subscriber_num||'%'
and GROUP_NAME like 'TEST' ||'%'
and csc.account_number=99995555
and csc.group_id= cgp.group_id
and csc.ver = 1
and cgp.ver = 1;
rank() over(order by length(cgp.group_name) desc)
將按照DESC
順序(最長到最短)中cgp.group_name
列中的值的長度順序(以最長到最短的順序)為每一行提供一個數值排名,並且不會嘗試打破cgp.group_name
。
rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc)
將按照ASC
順序中cgp.group_name
列中值的長度順序(最短到最長)為每一行賦予數值排名,並嘗試使用DESC
中csc.subscriber_num
中值的長度打破csc.subscriber_num
。順序(最長到最短)。
為了了解兩者之間的區別,這是一個針對一些示例數據的查詢(如果您不知道子查詢分解(又稱為“ with子句”,又稱為通用表表達式,又稱為CTE)),那么我強烈建議您進行研究! ):
with sample_data as (select 1 id, 1 val1, 1 val2, 13 val3 from dual union all
select 2 id, 1 val1, 2 val2, 12 val3 from dual union all
select 3 id, 1 val1, 3 val2, 11 val3 from dual union all
select 4 id, 1 val1, 4 val2, 10 val3 from dual union all
select 5 id, 2 val1, 4 val2, 9 val3 from dual union all
select 6 id, 2 val1, 3 val2, 8 val3 from dual union all
select 7 id, 2 val1, 2 val2, 7 val3 from dual union all
select 8 id, 2 val1, 1 val2, 6 val3 from dual union all
select 9 id, 3 val1, 2 val2, 5 val3 from dual union all
select 10 id, 3 val1, 6 val2, 4 val3 from dual union all
select 11 id, 3 val1, 2 val2, 3 val3 from dual union all
select 12 id, 4 val1, 7 val2, 2 val3 from dual union all
select 13 id, 4 val1, 8 val2, 1 val3 from dual)
select id,
val1,
val2,
val3,
rank() over (order by val1) rank1,
rank() over (order by val1, val2) rank2,
dense_rank() over (order by val1) dense_rank1,
dense_rank() over (order by val1, val2) dense_rank2
from sample_data;
ID VAL1 VAL2 VAL3 RANK1 RANK2 DENSE_RANK1 DENSE_RANK2
---------- ---------- ---------- ---------- ---------- ---------- ----------- -----------
1 1 1 13 1 1 1 1
2 1 2 12 1 2 1 2
3 1 3 11 1 3 1 3
4 1 4 10 1 4 1 4
8 2 1 6 5 5 2 5
7 2 2 7 5 6 2 6
6 2 3 8 5 7 2 7
5 2 4 9 5 8 2 8
9 3 2 5 9 9 3 9
11 3 2 3 9 9 3 9
10 3 6 4 9 11 3 10
12 4 7 2 12 12 4 11
13 4 8 1 12 13 4 12
另外,根據文檔 :
排名條件的值相等的行將獲得相同的排名。
將兩者結合在一起,並希望您能看到,因為您的行在order by子句的所有列中共享相同的值,因此它們具有相同的等級值。
例如,在上述查詢的結果中,id為(1,2,3,4)的行的rank1均為1,而id 9和11的rank2相同,而id 10的秩不同。您的排名標准,您獲得聯系的可能性越小(通常)。
這是否回答你的問題? 我還在上面的查詢中包括了等效的DENSE_RANK
函數,因此您可以看到等級編號的差異。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.