2017-05-05 62 views
-1

我有一個查詢從表中提取一些警報。如何排除兩行,如果有相同的值

我想用的名字「ALARM1」提取報警 「NODE_A」只要沒有「alarm_2」進行同樣的「NODE_A」

提前感謝!

NODE_ID | PORT_ID | ALARM_ID | CREATION_TIME 

NODE_A | PORT_A | ALARM_1 | CREATION_1 

NODE_B | PORT_B | ALARM_3 | CREATION_2 

NODE_A | PORT_A | ALARM_2 | CREATION_1 

NODE_C | PORT_C | ALARM_1 | CREATION_4 

NODE_C | PORT_C | ALARM_2 | CREATION_4 



select NODE_ID, 
     PORT_ID, 
    ALARM_ID, 
     CREATION_TIME 

from TABLE 

where ALARM_ID='ALARM_1' OR ALARM_ID='ALARM_2' OR ALARM_ID='ALARM_3' AND 
     CONCAT(NODE_ID, CREATION_TIME) <> CONCAT(NODE_ID, CREATION_TIME) 

,我想要的結果是:

NODE_ID | PORT_ID | ALARM_ID | CREATION_TIME 


NODE_B | PORT_B | ALARM_3 | CREATION_2 
+3

添加一些示例表數據和預期的結果 - 所有以及格式化文本。同時向我們展示您當前的查詢嘗試。 – jarlh

+0

桌子是什麼樣的?你厭倦了什麼SQL?你想要什麼結果?這個問題真的不會導致任何地方... – Manngo

+0

所以你想要在同一個節點沒有'alarm1'和'alarm2'都輸出?因爲你的示例中只有'node_b'滿足該條件 – Utsav

回答

0

你可以在一個計數> 1

select NODE_ID, 
      PORT_ID, 
      ALARM_ID, 
      CREATION_TIME 
    from TABLE 
    where NODE_ID not in (select NODE_ID 
          FROM TABLE 
          GROUP BY NODE_ID 
          having count(*) > 1) 

使用一個元組與非或使用連接如:

select NODE_ID, 
      PORT_ID, 
      ALARM_ID, 
      CREATION_TIME 
    from TABLE 
    inner join (
     select NODE_ID,count(*) 
     FROM TABLE 
     GROUP BY NODE_ID 
     having count(*) = 1) 
    ) t1 on t1.NODE_ID = TABLE.NODE_ID 
+0

謝謝,我試過了,但結果還包括我想排除的警報。如果我對同一個NODE有alarm_2和alarm_1,我想排除這兩行。 –

+0

答案更新,,讓我知道 – scaisEdge

+0

結果是一樣的,具有alarm_1的行保持不變,它不會只顯示帶有alarm_2的行。 –

0

使用count加上case

select node_id, port_id, alarm_id, creation_time 
    from (select t.*, 
       count(case when alarm_id = 'ALARM_2' then 1 end) 
        over (partition by node_id, creation_time) cnt 
      from t) 
    where cnt = 0 

測試:

with t (NODE_ID, PORT_ID, ALARM_ID, CREATION_TIME) as (
    select 'NODE_A', 'PORT_A', 'ALARM_1', 'CREATION_1' from dual union all 
    select 'NODE_B', 'PORT_B', 'ALARM_3', 'CREATION_2' from dual union all 
    select 'NODE_A', 'PORT_A', 'ALARM_2', 'CREATION_1' from dual union all 
    select 'NODE_C', 'PORT_C', 'ALARM_1', 'CREATION_4' from dual union all 
    select 'NODE_C', 'PORT_C', 'ALARM_2', 'CREATION_4' from dual 
    ) 
select node_id, port_id, alarm_id, creation_time 
    from (select t.*, 
       count(case when alarm_id = 'ALARM_2' then 1 end) 
        over (partition by node_id, creation_time) cnt 
      from t) 
    where cnt = 0 



NODE_ID PORT_ID ALARM_ID CREATION_TIME 
------- ------- -------- ------------- 
NODE_B PORT_B ALARM_3 CREATION_2 
+0

我不明白。在你使用的t表中(選擇'NODE_A','PORT_A','ALARM_1','CREATION_1'),但是我不知道NODE_A','PORT_A','ALARM_1','CREATION_1 –

+0

這些都是示例,測試數據,從您的問題中複製並在此處顯示,以顯示查詢的工作原理。你只需要從'select ...'開始的部分。對您的數據運行主查詢,將「T」更改爲您的表名並檢查結果。 –

+0

'over'不起作用。 –

相關問題