【问题标题】:CASE WHEN expression over multiple columns with conditions带有条件的多列上的 CASE WHEN 表达式
【发布时间】:2019-06-20 11:20:23
【问题描述】:

我有以下带有数据的输入列

Type1       Time1     Type2    Time2    Type3      Time3
Linehaul    3        Trans      2       Sort       1
Trans       5        Sort       2       Linehaul   2  

输出应该如下所示,末尾有一个新列,它从之前的列中选择排序时间 WHERE type 1 or type 2 or type 3 = Sort 单词 Sort 只能在前三种类型中的任何一种中出现一次。 代码应该出现在 SQL Oracle 中

Type1       time1    Type2  time2   Type3     time3    Trans time for sort
Linehaul    3        Trans  2       Sort      1         1
Trans       5        Sort   2       Linehaul  2         2

【问题讨论】:

    标签: sql oracle case


    【解决方案1】:

    你可以很简单地做到这一点。例如

    with tab as(
      select 'Linehaul' as c1,   3 as val1,  'Trans'as c2,      2 as val2,       'Sort' as c3,       1 as val3  from dual union all
      select 'Trans',       5,        'Sort',       2 ,      'Linehaul',   2   from dual
    
    )
    select t.* 
          ,case 
            when t.c1 = 'Sort' then val1
            when t.c2 = 'Sort' then val2
            when t.c3 = 'Sort' then val3
            else -1
              end as X
    from tab t
    

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2020-09-03
      • 2021-11-04
      • 2014-09-17
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 2018-02-01
      • 2012-09-20
      相关资源
      最近更新 更多