TA表有A,B,C,D四个字段。A字段和B字段都是数值。C字段可能为空也可能有字符串,D字段可能为空也可能有字符串。
现在需要根据TA表生成TB表。TB表有三个字段A,B,C。
要求:
如果TA.C为空且TA.D为空,则TB.C为空
如果TA.C为空但TA.D不为空,则TB.C=TA.D
如果TA.C不为空但TA.D为空,则TB.C=TA.C
如果TA.C不为空且TA.D不为空,则TB.C依据如下原则生成:
TA.A的值在1-3范围内且TA.B的值在1-3范围内,TB.C=NULL
TA.A的值在1-3范围内但TA.B的值不在1-3范围内,TB.C=TA.D
TA.A的值不在1-3范围内但TA.B的值在1-3范围内,TB.C=TA.C问1:能用一条SQL语句实现么?
问2:怎么实现!

解决方案 »

  1.   

    create table tb as
    select a,b,
      case when c is not null and d is not null then 
        case when a between 1 and 3 and not (b between 1 and 3) then d
          when not(a between 1 and 3)and b between 1 and 3 then c end
        when c is not null then c
        when d is not null then d  end c
    from ta
        
    你没说ta.a、b如果都不在1-3范围内怎么办。我这里默认处理是null