programAll
programAll

Reputation: 29

Oracle SQL query joining same table

I have a table like this:

                 items
id            old_new     object 

1               o         pen
2               n         house
3               o         dog
4               o         cat
5               n         carrot

I would like the select return:

id    new_object     old_object

1        null          pen
2        house        null
3        null         dog
4        null         cat  
5        carrot       null 

Do I need to use an outer join on the same table?

Upvotes: 0

Views: 56

Answers (1)

user330315
user330315

Reputation:

No join needed:

select id, 
       case when old_new = 'n' then object end as new_object,
       case when old_new = 'o' then object end as old_object
from the_table
order by id;

Upvotes: 2

Related Questions