![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi all, DB2 LUW 9.5 fixpack 5. Scenario: select<result> from A, B where .... If A contains at least one row, return A.* else B.* Is there a way to do this in SQL? Thanks in Advance. Bruno. |
#3
| |||
| |||
|
|
Assuming that columns in table A and B are equivalent. Select * from A where <cond and 1 >= (select count(*) from A where <cond> ) union all select * from B where <cond and 0 = (select count(*) from A where <cond> ) Select * from A |
#4
| |||
| |||
|
|
Select * from A *where <cond union all select * from B *where <cond * *and NOT EXISTS * * * *(select 0 from A where <cond> ) In this case, UNION ALL can be replaced by FULL OUTER JOIN, like this: |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Using rank() and union all: select * From ( select *, rank() over(order by table_order) as rank from( Select *, 1 as table_order from A *where <cond union all select *, 2 as table_order from B *where <cond ) x ) y where rank = 1 You'll probably want to call out the columns in the outer select specifically (to at least get rid of the table_order and rank columns). |
![]() |
| Thread Tools | |
| Display Modes | |
| |