使用decode / CASE从两个表的字段组合构建记录

时间:2014-05-07 17:52:05

标签: sql oracle oracle11g

鉴于以下表格:

表A

id
field1
field2
field3
field4
filed5
field6
field7
field8
field9

表B

id
field1
field2
field3
field4
field5
statusField

我想构建一个由上述两个表中的字段组成的记录。完整记录由以下字段组成

tableA.field1 or tableB.field1
tableA.field2 or tableB.field2
tableA.field3 or tableB.field3
tableA.field4 or tableB.field4
tableA.field5 or tableB.field5
                 tableB.field6
             tableB.field7
             tableB.field8
             tableB.field9

如果tableB.statusField = 'Valid'的值使用tableB中的值,则使用TableA中的值。 始终使用字段6到9,因为它们仅存在于tableB中。

我可以考虑两种方法来构建此查询:

方法1

select decode(tableB.statusField, 'Valid', tableB.field1, tableA.field1), 
    decode(tableB.statusField, 'Valid', tableB.field1, tableA.field1), 
    decode(tableB.statusField, 'Valid', tableB.field2, tableA.field2), 
    decode(tableB.statusField, 'Valid', tableB.field3, tableA.field3), 
    decode(tableB.statusField, 'Valid', tableB.field4, tableA.field4), 
    decode(tableB.statusField, 'Valid', tableB.field5, tableA.field5),tableB.field6, tableB.field7, tableB.field8, tableB.field9
from tableA, tableB
where tableA.id = tableB.id

方法2

Select 
    case tableB.statusField when 'valid' then tableB.field1 else tableA.field1 end,
    case tableB.statusField when 'valid' then tableB.field1 else tableA.field1 end,
    case tableB.statusField when 'valid' then tableB.field2 else tableA.field2 end,
    case tableB.statusField when 'valid' then tableB.field3 else tableA.field3 end,
    case tableB.statusField when 'valid' then tableB.field4 else tableA.field4 end,
    case tableB.statusField when 'valid' then tableB.field5 else tableA.field5 end,
    tableB.field6, tableB.field7, tableB.field8, tableB.field9
from tableB, tableA
where tableB.id = tableA.id

我认为上述方法之一可行,但我有这种感觉,你觉得这不是最好的方法。

是否有更好的或其他替代方法来实现相同的目标?我想要构建的记录由125个字段组成,所以我希望避免使用125个解码/ case语句。

(我使用的是Oracle 11G)

由于

2 个答案:

答案 0 :(得分:1)

作为替代方案,您可以尝试使用UNION:

Select 
    tableB.field1,
    tableB.field1,
    tableB.field2,
    tableB.field3,
    tableB.field4,
    tableB.field5,
    tableB.field6, tableB.field7, tableB.field8, tableB.field9
from tableB, tableA
where tableB.id = tableA.id and tableB.statusField='Valid'
union
Select 
    tableA.field1,
    tableA.field1,
    tableA.field2,
    tableA.field3,
    tableA.field4,
    tableA.field5,
    tableA.field6, tableB.field7, tableB.field8, tableB.field9
from tableB, tableA
where tableB.id = tableA.id and tableB.statusField<>'Valid'

答案 1 :(得分:1)

我会使用left outer join并合并:

select coalesce(b.field1, a.field1) as field1,
       coalesce(b.field2, a.field2) as field2,
       coalesce(b.field3, a.field3) as field3,
       coalesce(b.field4, a.field4) as field4,
       coalesce(b.field5, a.field5) as field5,
       coalesce(b.field6, a.field6) as field6,
       b.field7, b.field8, b.field9, b.field10       
from tableA a left outer join
     tableB b
     on a.id = b.id and
        b.statusField = 'Valid';

这假设b中的所有字段都没有NULL个值。否则,您需要使用case代替:

select (case when b.statusField = 'Valid' then b.field1 else a.field1 end) as field1, 
       . . .