Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Oracle > Use a subquery instead of a Union?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-07-08, 14:27
turk99 turk99 is offline
Registered User
 
Join Date: Jan 2004
Posts: 82
Use a subquery instead of a Union?

I have two tables, one has security applied to it and the other does not. I need to pull information from both. There could be redundant data between the tables.

Secure : Log in and see only job data for your department
Unsecure : Some of the same columns as Secured, but you can see outside your department.

What I need to do is combine the unique results. However, because "unsecure" does not have all the columns that "secure" does, I have to put in placeholders for the union to work. Because of the placeholders, the union thinks every result is unique.

Code:
SELECT effective_dt, action,compensation FROM secure WHERE employe_id=#employe_id# UNION SELECT effective_dt,action,TO_NUMBER('') FROM unsecure WHERE employe_id=#employe_id#


Results:

1/1/2008 HIRE 52,000
1/1/2008 HIRE
8/5/2008 PAYI
9/9/2008 TERM 56,000
9/9/2008 TERM

What I want to display is the 1/1 and 9/9 rows that show compensation and the 8/5 row.

Is there some kind of subquery I could do?

Last edited by turk99 : 07-07-08 at 14:30.
Reply With Quote
  #2 (permalink)  
Old 07-07-08, 15:12
turk99 turk99 is offline
Registered User
 
Join Date: Jan 2004
Posts: 82
To put it another way, what I want to do is query the secure table, and if there are additional rows not in secure that are located in unsecure, I want to append results.
Reply With Quote
  #3 (permalink)  
Old 07-07-08, 17:01
chuck_forbes chuck_forbes is offline
Registered User
 
Join Date: Dec 2003
Posts: 847
Code:
select f1, f2, f3 from table1 union all select f1, f2, f3 from table2 t2 where not exists (select 1 from table1 t1 where t1.f1 = t2.f1 and t1.f2 = t2.f2)
--=cf
Reply With Quote
  #4 (permalink)  
Old 07-07-08, 17:06
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 2,715
Alternatively, you could use NOT IN instead of NOT EXISTS:
Code:
SELECT effective_dt, action, compensation FROM secure UNION (SELECT effective_dt, action, to_number('') FROM unsecure WHERE (effective_dt, action) NOT IN (SELECT effective_dt, action FROM secure) );
Reply With Quote
  #5 (permalink)  
Old 07-08-08, 11:48
chuck_forbes chuck_forbes is offline
Registered User
 
Join Date: Dec 2003
Posts: 847
Note that you might be able to exchange UNION for UNION ALL when you switch over to using a subquery. If that's possible, it will yield better performance. --=cf
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On