Sayandip Ghatak
Sayandip Ghatak

Reputation: 303

Conditional Select from different tables

I have a requirement :

There is a column CNT from a table A .

If the value of CNT > 0

I have to choose column SERIAL and DESCRIPTION

from table B else SERIAL and DESCRIPTION

from table C .

How can I do this in Oracle

Upvotes: 2

Views: 2305

Answers (1)

David Faber
David Faber

Reputation: 12495

You'll want to use a UNION:

SELECT a.cnt, b.serial, b.description
  FROM a INNER JOIN b
    ON a.key = b.key
 WHERE a.cnt > 0
 UNION ALL
SELECT a.cnt, c.serial, c.description
  FROM a INNER JOIN c
    ON a.key = c.key
 WHERE a.cnt <= 0;

Hope this helps.

Upvotes: 8

Related Questions