Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 25th, 2004, 06:33 PM
sriramc sriramc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 sriramc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Is this possible with a Single SQL Query?

I have 3 identical tables. Let us say that it just contains 2 columns company_code and price. Is it possible to get the first occurrence of the price for example company_code='MSFT' from these tables.

The catch is the query should display the first occurence only. If the table 1 contain the price for MSFT then the output should only display the value from table 1 and ignore table 2 and 3, else it should check in table 2 and ignore table 3 or else it should display from table 3.

Reply With Quote
  #2  
Old June 26th, 2004, 02:09 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
I guess,

SELECT distinct company_code, price
FROM table1 a, table2 b, table3 c
WHERE a.comapny_code = 'MSFT'
OR b.company_code = 'MSFT'
OR c.comapny_code = 'MSFT'
/

Reply With Quote
  #3  
Old June 26th, 2004, 04:44 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
the best way to do so is using union set operator in your query as given below:

SELECT company_code,price from tabe1
WHERE company_code ='MSTF'
UNION
SELECT company_code,price from tabe2
WHERE company_code ='MSTF'
UNION
SELECT company_code,price from tabe3
WHERE company_code ='MSTF'

UNION set operator not only merge the data set of query based on different table but also eliminate the duplicate records. If you want to see the duplicate record as well then use UNION ALL instead of using UNION.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Is this possible with a Single SQL Query?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway