0

In a oracle i'm using a Decode Statement as below. For security reasons i don't want the code to have the hardcoded values and i plan to create a new lookup table.

  Select CONCAT( Decode(A.COUNTRY_INITIAL,
                        'A','America',
                        'B','Brazil',
                        'F','FINLAND',
                        NULL),
                 Decode(A.ADD_VALUE,
                        'M','YES',
                        NULL))
  from  (
      Select SUBSTR(COUNTRY_BASE, -1,1) as COUNTRY_INITIAL,
             SUBSTR(IS_VALUED, -1,1) as ADD_VALUE
      from TBL1
  )A

Refernece Table
*******************
Clmn1   Clmn2   Clmn3
--------------------------
cntry1  A       America
cntry2  B       Brazil
cntry3  F       Finland
Value1  M       YES

Could you please let me know how i can incorporate this in the decode logic. Also fyi im using this CODE SNIPPET in a Oracle Function.

4

3 回答 3

1

如果要将查找信息存储在表中,则不会使用DECODE. 你会加入两张桌子

SELECT ref.clmn3
  FROM tbl1 t1
       LEFT OUTER JOIN <<reference table>> ref
         ON( substr(t1.country_base, -1, 1) = ref.clmn2 )

由于您DECODE有 a NULL,我猜您期望tbl1参考表中的某些行不会有匹配的行,所以我猜您想要 aLEFT OUTER JOIN而不是 a INNER JOIN

于 2013-09-13T05:09:23.677 回答
0

如果您在表中有这些详细信息,您可以简单地使用连接来获得所需的输出。

select t1.COUNTRY_BASE,ref.Clmn3,ref1.Clmn3
frorm TBL1 t1
left outer join reftable ref
on SUBSTR(t1.COUNTRY_BASE, -1,1)=ref.Clmn2  
left outer join reftable ref1
on SUBSTR(t1.IS_VALUED, -1,1)=ref.Clmn2;
于 2013-09-13T05:15:03.383 回答
0

如果您使用单独的表来存储值,则不需要解码功能。您可以简单地连接这两个表。

select a.country_base,
       a.is_valued,
       b.clmn3,
       c.clmn3
  from tbl1 a left outer join reference_table b
              on (substr(a.country_base, -1, 1) = b.clmn2
              and b.clmn1 like 'cntry%'  --extra clause needed if there are same country and value codes
              )
              left outer join reference_table c
              on (substr(a.is_valued, -1, 1) = c.clmn2
              and c.clmn1 like 'Value%'  --extra clause needed if there are same country and value codes
              );
于 2013-09-13T05:08:23.207 回答