如果还存在该特定条目的另一个条目/值,如何获取另一个“标记的”值

时间:2018-10-16 15:56:37

标签: sql oracle

因此,我遇到了一个问题,即我的当前系统正在标记在单独的数据加载过程中“替换掉”并计算出的任何值。一些示例数据如下:

Value            Brand             Hour                  Code      Type
  5                G           10/16/2018 01:00 AM      'ABC'         A
 NULL              G           10/16/2018 01:00 AM      'MIS'         A
  12               G           10/16/2018 02:00 AM      'ABC'         A
  2                B           10/16/2018 01:00 AM      'ABC'         B
 NULL              B           10/16/2018 01:00 AM      'MIS'         B

我想要的是什么时候有两个(或更多)条目,我希望创建的视图能够捕获具有<>'ABC'的data_code但仍然具有data_code ='ABC'的NULL值(如如下所示)

Value            Brand             Hour                 Code       Type
 NULL              G           10/16/2018 01:00 AM      'ABC'         A
  12               G           10/16/2018 02:00 AM      'ABC'         A
 NULL              B           10/16/2018 01:00 AM      'ABC'         B

我尝试了以下方法,但不确定是否正确:(是否有更好的方法?)

CREATE OR REPLACE VIEW 'VIEW_1' as
select CASE WHEN Data_code <> 'ABC'
            THEN select MIN(value_nb)
                   from Table_1
                   group by Brand, Hour, Type
            ELSE value
             END            VALUE
     , Brand
     , Hour
     , 'ABC'
     , Type
  from Table_1;

1 个答案:

答案 0 :(得分:0)

要总结评论,您需要没有匹配的“ MIS”行的“ ABC”行(基于品牌/小时/类型的组合);加上同样具有匹配的“ ABC”的“ MIS”行,但代码更改为“ ABC” ...

您可以将两个查询合并在一起以获得行的那些子集:

select value
     , brand
     , hour
     , code
     , type
  from table_1 t1
  where code = 'ABC'
  and not exists (
    select *
      from table_1 t1_sub
     where t1_sub.brand = t1.brand
       and t1_sub.hour = t1.hour
       and t1_sub.type = t1.type
       and t1_sub.code = 'MIS'
  )
union all
select value
     , brand
     , hour
     , 'ABC'
     , type
  from table_1 t1
  where code = 'MIS'
  and exists (
    select *
      from table_1 t1_sub
     where t1_sub.brand = t1.brand
       and t1_sub.hour = t1.hour
       and t1_sub.type = t1.type
       and t1_sub.code = 'ABC'
  );

     VALUE BRAND HOUR                CODE TYPE
---------- ----- ------------------- ---- ----
        12 G     2018-10-16 02:00:00 ABC  A   
           G     2018-10-16 01:00:00 ABC  A   
           B     2018-10-16 01:00:00 ABC  B   

如果每个“ MIS”必须始终有匹配的“ ABC”,则可以简化第二个分支:

select value
     , brand
     , hour
     , code
     , type
  from table_1 t1
  where code = 'ABC'
  and not exists (
    select *
      from table_1 t1_sub
     where t1_sub.brand = t1.brand
       and t1_sub.hour = t1.hour
       and t1_sub.type = t1.type
       and t1_sub.code = 'MIS'
  )
union all
select value
     , brand
     , hour
     , 'ABC'
     , type
  from table_1 t1
  where code = 'MIS';

     VALUE BRAND HOUR                CODE TYPE
---------- ----- ------------------- ---- ----
        12 G     2018-10-16 02:00:00 ABC  A   
           G     2018-10-16 01:00:00 ABC  A   
           B     2018-10-16 01:00:00 ABC  B   

您还可以通过多种方式使用分析函数对表格进行一次点击;这只是一个使空值优先于非空值的示例:

select value
     , brand
     , hour
     , code
     , type
  from (
    select value
         , brand
         , hour
         , 'ABC' as code
         , type
         , row_number() over (partition by brand, hour, type order by value nulls first) as rn
      from table_1
  )
 where rn = 1;

     VALUE BRAND HOUR                CODE TYPE
---------- ----- ------------------- ---- ----
           B     2018-10-16 01:00:00 ABC  B   
           G     2018-10-16 01:00:00 ABC  A   
        12 G     2018-10-16 02:00:00 ABC  A