views:

1575

answers:

1

Hy guys,

can anybody please help me with a subquery in Oracle database 10g? I need to extract the values for a column in the first table as value of another column in the second table. I currently use this statement:

SELECT 
CASE WHEN A.column1 = 'A' THEN 'aaa'
WHEN A.column1 = 'B' THEN 'bbb'
.......
WHEN A.column1 = 'X' THEN 'xxx'
ELSE 'bad' END AS COLUMN1, A.* 
FROM TRANSACTION_TABLE A,  CATEGORY_TABLE B 
WHERE A.column1 IS NOT NULL 
AND A.column1 <> ' '

This is not an elegant approach, so I'm trying to use a subselect from CATEGORY_TABLE B like the following:

SELECT A.column1, A.* 
FROM TRANSACTION_TABLE A,  CATEGORY_TABLE B 
WHERE A.column1 IS NOT NULL 
AND A.column1 = B.column_b_1 
AND A.column1 <> ' '
AND A.column1 IN (SELECT B.column_b_1_descr FROM CATEGORY_TABLE B 
WHERE B.FIELDNAME = 'column1' AND A.column1 = B.column_b_1)

So, I cannot get any results by using the subquery and don't want to continue using the CASE against many conditions, just want to replace the A.column1 values with the descriptive values from B.column_b_1_descr , as they're easier to read. I would appreciate any feedback. Thanks

+1  A: 

Unless I'm misunderstanding your question...

CATEGORY_TABLE:
  name | value
   A      aaa
   B      bbb
   C      ccc
 ...


SELECT B.value AS COLUMN1, A.\* 
FROM TRANSACTION\_TABLE A, CATEGORY\_TABLE B 
WHERE A.column1 = B.name

or

SELECT t2.value as COLUMN1, t1.\* 
FROM TRANSACTION\_TABLE t1 
INNER JOIN CATEGORY\_TABLE t2 ON t1.column1 = t2.name;

The where clause isn't needed, since an inner join automatically excludes rows with null values or no matches.

Tim Sylvester
If you want to retain 'bad' values, turn this into an OUTER JOIN and change the first part of the SELECT to "SELECT Coalesce(t2.value, 'bad') as COLUMN1, ...
Dave W. Smith