dbTalk Databases Forums  

SQL Result from A or B

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss SQL Result from A or B in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
brunoalsantos
 
Posts: n/a

Default SQL Result from A or B - 08-02-2010 , 08:41 AM






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.

Reply With Quote
  #2  
Old   
danfan46
 
Posts: n/a

Default Re: SQL Result from A or B - 08-02-2010 , 09:20 AM






On 2010-08-02 15:41, brunoalsantos wrote:
Quote:
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.
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> )

/dg

Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: SQL Result from A or B - 08-02-2010 , 09:32 AM



Quote:
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
where <cond>
union all
select * from B
where <cond>
and NOT EXISTS
(select 0 from A where <cond> )

Reply With Quote
  #4  
Old   
Tonkuma
 
Posts: n/a

Default Re: SQL Result from A or B - 08-02-2010 , 10:15 AM



Quote:
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:

SELECT COALESCE(a.col_1 , b.col_1) AS col_1
, COALESCE(a.col_2 , b.col_2) AS col_2
....
, COALESCE(a.col_n , b.col_n) AS col_n
FROM (SELECT * FROM A WHERE <cond>) A
FULL OUTER JOIN
(SELECT *
FROM B
WHERE <cond>
AND NOT EXISTS
(SELECT 0 FROM A WHERE <cond>)
) B
ON 0=0
;

Reply With Quote
  #5  
Old   
ChrisC
 
Posts: n/a

Default Re: SQL Result from A or B - 08-02-2010 , 01:33 PM



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).

Reply With Quote
  #6  
Old   
brunoalsantos
 
Posts: n/a

Default Re: SQL Result from A or B - 08-03-2010 , 07:00 AM



On 2 ago, 15:33, ChrisC <cunningham... (AT) gmail (DOT) com> wrote:
Quote:
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).
Thank you danfan46, Tonkuma and Chris.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.