Oracle:根据列的值确定结果的优先级

问题描述 投票:0回答:1

我有一个数据集,其中第一列中有重复的ID。我希望根据第二列的值为每个ID获取一行数据。数据看起来像这样:

ID   Info_Source   Prior?
A        1           Y
A        3           N
A        2           Y
B        1           N
B        1           N
B        2           Y
C        2           N
C        3           Y
C        1           N

具体来说,标准将要求基于第二列的值(3个最高优先级;然后是1;最后2个)进行优先级排序:如果“Info_Source”列的值为3,则返回该行;如果给定ID的第二列中没有3,则查找1并且如果找到则返回该行;最后,如果没有与ID关联的3或1,请搜索2并返回该行的ID。

对于每个ID,所需的结果将是一行,结果数据将是:

ID   Info_Source   Prior?
A        3           N
B        1           N
C        3           Y
oracle
1个回答
1
投票

row_number()over()通常可以很好地有效地解决这些需求,例如:

select ID, Info_Source, Prior
from (
      select ID, Info_Source, Prior
      , row_number() over(partition by id order by Info_source DESC) as rn
     )
where rn = 1

要优先处理第二列的值(3;然后是1,然后是2),请使用case表达式将原始值更改为您需要的顺序。

select ID, Info_Source, Prior
from (
      select ID, Info_Source, Prior
      , row_number() over(partition by id 
                          order by case when Info_source = 3 then 3
                                        when Infor_source = 1 then 2
                                        else 1 end DESC) as rn
     )
where rn = 1
© www.soinside.com 2019 - 2024. All rights reserved.