2016-03-05 31 views
0

我有一个表甲骨文 - 选择具有多个条件

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

OR

  1. 如果有多个条目具有相同ORIGSSID$选择一个具有最大ID
在我的情况

预期的结果是:

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

请帮我构建SELECT表达式..

+0

我认为1049/1049也应该在结果中,根据你的规则。 –

+0

谢谢Gordon,更具体一点1.如果'ID == ORIGSSID $'和'count(ORIGSSID $)== 1',那么1049/1049应该不符合我的标准 – Alexander

回答

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; 
+0

请问你能解释一下'from如果我在你的例子中替换't' - >'TABLE.SUBTABLE',我收到一个错误:“ORA-00933:SQL命令没有正确结束“ – Alexander

+0

它的工作原理,谢谢! – Alexander