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

时间:2017-11-08 00:13:07

标签: oracle

我有一个数据集,其中第一列中有重复的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”#39;列的值为3,返回该行;如果给定ID的第二列中没有3,则查找1并且如果找到则返回该行;最后,如果没有与ID关联的3或1,请搜索2并返回该行的ID。

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

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

1 个答案:

答案 0 :(得分: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