如何根据最大和最小序列

时间:2015-08-09 16:40:51

标签: sql oracle

我有2张以下结构表。现在,我想生成一个与表t1相同的报告,其备注栏中包含'匹配'或者'不匹配'值。  选择备注列的条件是,当t1表max和min时,每个Ringid的SequenceNo列NodeName值出现在t2中,则所有备注列值匹配,否则不匹配每个ringid集。

表:t1

RingID  SequenceNo  NodeName     ISDName    Remarks
1         1          n1           gx1   
1         2          n2           gx2   
1         3          n3           gx1   
1         4          n4           gx3   
2         1          n6           gx1   
2         2          n7           gx3   
2         3          n8           gx5   
2         4          n9           gx6   
2         5          n10          gh6   

表:t2

ID  NodeName
1   n1
2   n4
3   n6
4   n7

现在只为戒指匹配,因为在t2表中没有第1和第4个值

1 个答案:

答案 0 :(得分:0)

使用函数first_valuecase ... when ...子句:

select t1.ringid, sequenceno, nodename, isdname, 
    case when exists (select 1 from table2 where nodename=t1.mn)
          and exists (select 1 from table2 where nodename=t1.mx)
         then 'matched' else 'not matched' 
    end remarks
  from 
    (select table1.*, 
        first_value(nodename) over (partition by ringid order by sequenceno) mn, 
        first_value(nodename) over (partition by ringid order by sequenceno desc) mx 
      from table1) t1

SQLFiddle demo