2016-03-05 48 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