Home » SQL & PL/SQL » SQL & PL/SQL » Translating data
Translating data [message #11180] Wed, 10 March 2004 11:16 Go to next message
Dave
Messages: 92
Registered: August 1999
Member
I need to build a translation table and the assocaited SQL that will take rows from one table, look them up in a translation table, and assign to them a translated code.  The complication is that I need to have some 'default' codes that apply when other don't.  Here's what I'm trying to do:

Data_Table Fields

- column: ID -
- column:RT -
- column:RC -

- row1 -
- rt1 -
- rc1 -

- row2 -
- rt1 -
- rc1 -

- row3 -
- rt1 -
- rc2 -

- row3 -
- rt1 -
- rc2 -

- row4 -
- rt1 -
- rc3 -

- row5 -
- rt1 -
- -

- row6 -
- rt2 -
- -

Translation_Table

- column: RT -
- column: RC -
- column: trans_code -
- comment -

- rt1 -
- rc1 -
- A -
- -

- rt1 -
- rc2 -
- B -
- -

- rt1 -
- <any other> -
- C -
- <any other> is not data, just the logic.  I can put anything I want in this field, or leave it null, or whatever -

- -
- -
- -
- -

- -
- -
- -
- -

 So that when I run the sql to translate the data table, I get a result set that looks like:

- column: id -
- column: trans_code -

- row1 -
- A -

- row2 -
- A -

- row3 -
- B -

- row4 -
- C -

- row5 -
- C -

- row6 -
- FAILED TO TRANSLATE (or something like this) -

- -
- -

Obviously, there are lots of other fields on the data rows, but this is the skeleton of what I need to achieve.  Also, my real translation will involve four fields, not just the two I have illustrated here.  But if I can get a structure for this I can probably extrapolate it out to more fields. 

I can build the translate table any way I want, but I only have about 30 trans_code values, and I'll be searching against thousands of possible combinations of RT and RC.  I would prefer not to have to build every single RT, RC combination into the trans_table, but hopefully only the 30 or so that will produce my results.  Maybe I won't be able to get away with that.

Translating into code A or B is easy, simple joins.  But how do I then say 'whatever is left that matches RT1 goes into translation C'?

There are about 3,000,000 rows in the data_table, and it will grow to 10 million or so before we start to archive out.  This process will need to run every night so it has to be somewhat efficient, but if we get into a performance issue we can probably switch to incremenetally updating only the newly added rows.

This is running against an Oracle 9i database.

Anybody want to help tackle this one?  Thanks,

Dave
Re: Translating data [message #11181 is a reply to message #11180] Wed, 10 March 2004 12:14 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
What about handling the lookup as a scalar subquery instead of a join? Then we can do a nested evaluation (look for exact match on both columns, fall back to look to exact match on rt but no match on rc, fall back to failure message):

sql>select * from data_table;
 
       ID        RT        RC
--------- --------- ---------
        1         1         1
        2         1         1
        3         1         2
        4         1         3
        5         1
        6         2
 
6 rows selected.
 
sql>select * from translation_table;
 
       RT        RC T
--------- --------- -
        1         1 A
        1         2 B
        1           C
 
3 rows selected.
 
sql>select id, rt, rc,
  2         nvl((select trans_code
  3                from translation_table tt
  4               where tt.rt = dt.rt
  5                 and tt.rc = dt.rc), nvl((select trans_code
  6                                            from translation_table tt
  7                                           where tt.rt = dt.rt
  8                                             and tt.rc is null), 'Failed to translate')) trans_code  
  9    from data_table dt
 10   order by id;
 
       ID        RT        RC TRANS_CODE
--------- --------- --------- -------------------
        1         1         1 A
        2         1         1 A
        3         1         2 B
        4         1         3 C
        5         1           C
        6         2           Failed to translate
 
6 rows selected.
Re: Translating data [message #11183 is a reply to message #11181] Wed, 10 March 2004 12:29 Go to previous message
Dave
Messages: 92
Registered: August 1999
Member
WOW! You make it look too easy. I'll need to go through this in detail (I posted in the newbie section for a reason), but this looks great.

Thanks for the help and the quick response.
Dave
Previous Topic: help with procedure
Next Topic: Sql-loader Perofrmance Question
Goto Forum:
  


Current Time: Fri Apr 19 18:55:15 CDT 2024