tags:

views:

34

answers:

2

I have an ORACLE table which includes the following fields:

FieldA1 NUMBER(10,0)
FieldA2 VARCHAR2(40 BYTE)
FieldB1 NUMBER(10,0)
FieldB2 VARCHAR2(40 BYTE)

How do I write an ORACLE view that reads from the table and if the following condition is true:

FieldA1 matches FieldB1 OR FieldA2 matches FieldB2

outputs the character 'Y' as one of the columns and if the condition above isn't true outputs 'N' ?

+4  A: 
CREATE VIEW view_name
AS
SELECT fieldA1, 
       fieldA2, 
       fieldB1, 
       fieldB2,
       (CASE WHEN fieldA1 = fieldB1 THEN 'Y'
             WHEN fieldA2 = fieldB2 THEN 'Y'
             ELSE 'N'
         END) column_alias
  FROM table_name
Justin Cave
+3  A: 

In addition to Justin Cave's answer:

Since the fields are not specified as NOT NULL, it may be convenient to interpret "fieldA match fieldB" as "fieldA = fieldB, or both fieldA and fieldB are nulls".
In this case replace fieldA = fieldB by
(fieldA = fieldB) or (fieldA is null and fieldB is null).

egorius