如果具有相同的值,如何排除两行

时间:2017-05-05 11:56:52

标签: sql oracle

我有一个查询从表中提取一些警报。

我想提取名为“alarm1”的闹钟 “NODE_A”仅在相同“NODE_A”没有“alarm_2”的情况下

提前致谢!

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

2 个答案:

答案 0 :(得分:0)

您可以使用不在计数中的元组&gt; 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

答案 1 :(得分:0)

countcase结合使用:

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