Oracle - 具有多个条件的SELECT

时间:2016-03-05 18:33:40

标签: sql oracle

我有一张桌子

NAME                              ID ORIGSSID$
------------------------------ ----- ---------
Reproducibility Renamed 2       1287      1088
Reproducibility Renamed         1284      1088
NoiseDrift                      1049      1049
Reproducibility_8h              1131      1131
Reproducibility                 1088      1088
Noise and Drift                 1263      1049

我需要根据两个标准选择行:

  1. 如果IDORIGSSID$相等且COUNT(ORIGSSID$) == 1
  2. OR

    1. 如果有多个条目使用相同的ORIGSSID$,请选择最大ID
    2. 的条目

      我的预期结果是:

      NAME                              ID ORIGSSID$
      ------------------------------ ----- ---------
      Reproducibility Renamed 2       1287      1088
      Reproducibility_8h              1131      1131
      Noise and Drift                 1263      1049
      

      请帮我构建SELECT表达式..

1 个答案:

答案 0 :(得分:2)

根据您的两个条件,逻辑似乎是:

select name, id, ORIGSSID$
from (select t.*,
             max(id) over (partition by ORIGSSID$) as maxid,
             count(*) over (partition by ORIGSSID$) as cnt
      from TABLE.SUBTABLE t
     ) t
where id = ORIGSSID$ or (id = maxid and cnt > 1);

根据您的示例结果,您似乎想要:

select name, id, ORIGSSID$
from (select t.*,
             max(id) over (partition by ORIGSSID$) as maxid
      from TABLE.SUBTABLE t
     ) t
where id = maxid;
相关问题