If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > selecting data from 3 tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-12, 00:56
shajju shajju is offline
Registered User
 
Join Date: Aug 2008
Posts: 340
selecting data from 3 tables

Hi

Hope my query will be simple to understand.

I have 3 tables in the same schema which have the same primary keys. Each has 3 primary keys.
I need to select one column each from each table but if I use aliases, I get too many rows.
Code:
select a.datetime, a.bbb, a.ccc, a.col1+b.col1+c.col1 abc
from table1 a, table2 b, table3 c
where a.bbb=b.bbb
and b.bbb=c.bbb
and a.ccc=b.ccc
and b.ccc=c.ccc
Any input is much appreciated.

Regards
Reply With Quote
  #2 (permalink)  
Old 02-13-12, 01:08
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Quote:
Each has 3 primary keys
That's impossible.

Perhaps you should post a test case (CREATE TABLE & INSERT INTO several sample records) so that we could see what's you really have.

Just wondering: what is "I get too many rows" for you? Did you mean to say that query returns (for example) 1000 records while you *know* it should return only 10, or did Oracle raise TOO-MANY-ERRORS error?
Reply With Quote
  #3 (permalink)  
Old 02-13-12, 03:50
shajju shajju is offline
Registered User
 
Join Date: Aug 2008
Posts: 340
What I meant to say was each table has the same primary keys:
'Datetime', 'bbb' and 'ccc'.

The number of rows returned should only be 1500 but millions of records are being returned.
Reply With Quote
  #4 (permalink)  
Old 02-13-12, 05:09
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,205
You should add Datetime to WHERE clause.

For example:

sample data:
Code:
table 1            | table 2
-------------------+-------------------
bbb ccc datetime   | bbb ccc datetime
-------------------+-------------------
1   1   2012-02-11 | 1   1   2012-02-11
1   1   2012-02-12 | 1   1   2012-02-12
-   -   -          | 1   1   2012-02-13
Result of
FROM table1 a , table2 b
WHERE a.bbb = b.bbb AND a.ccc = b.ccc
Code:
table 1            | table 2
-------------------+-------------------
bbb ccc datetime   | bbb ccc datetime
-------------------+-------------------
1   1   2012-02-11 | 1   1   2012-02-11
1   1   2012-02-11 | 1   1   2012-02-12
1   1   2012-02-11 | 1   1   2012-02-13
1   1   2012-02-12 | 1   1   2012-02-11
1   1   2012-02-12 | 1   1   2012-02-12
1   1   2012-02-12 | 1   1   2012-02-13
Result of
FROM table1 a , table2 b
WHERE a.bbb = b.bbb AND a.ccc = b.ccc AND a.datetime = b.datetime
Code:
table 1            | table 2
-------------------+-------------------
bbb ccc datetime   | bbb ccc datetime
-------------------+-------------------
1   1   2012-02-11 | 1   1   2012-02-11
1   1   2012-02-12 | 1   1   2012-02-12

Last edited by tonkuma; 02-13-12 at 05:26. Reason: Add sample.
Reply With Quote
  #5 (permalink)  
Old 02-13-12, 06:44
shajju shajju is offline
Registered User
 
Join Date: Aug 2008
Posts: 340
Ofcourse. I could I miss that! Must admit, dbforums never lets me down

Thanks guys.
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On