首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >oracle查询中的秩()和以上()

oracle查询中的秩()和以上()
EN

Stack Overflow用户
提问于 2016-04-18 13:31:45
回答 2查看 243关注 0票数 0

我需要理解这两个oracle查询之间的区别,特别是在rank() over(order by length(cgp.group_name) desc)rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc) ranking之间。

我搜索了谷歌的排名,并理解如下:

rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc) ranking:一个排名列将显示在结果中,根据group_name的最大匹配值为1、2、3。

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

发布于 2016-04-18 13:56:06

代码语言:javascript
复制
rank() over(order by length(cgp.group_name) desc)

将按照cgp.group_name列中值的长度的顺序(最长到最短)给每一行一个数字级,并且不会试图中断连接。

代码语言:javascript
复制
rank() over(order by length(cgp.group_name), length(csc.subscriber_num) desc)

将按照cgp.group_name列中值的长度(最短到最长)的顺序给每一行一个数字排序,并尝试使用csc.subscriber_num中值的长度(从最长到最短)来断开联系。

票数 1
EN

Stack Overflow用户

发布于 2016-04-18 13:58:46

为了了解不同之处,这里有一个针对某些示例数据的查询(如果您不知道子查询保理(也称为“with子句”,也称为公共表表达式,即CTE),那么我强烈建议您研究它!):

代码语言:javascript
复制
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子句中的所有列之间共享相同的值,所以它们具有相同的秩值。

例如,在上面的查询结果中,id in ( 1,2,3,4)中的行都有1的rank1,而id 9和11有相同的rank2,但是id 10有不同的等级),所以,排序标准越具体,就越不可能得到联系(一般情况下)。

这能回答你的问题吗?我还在上面的查询中包含了等价的DENSE_RANK函数,因此您可以看到等级编号的差异。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36695507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档