Moin,
First, the relationships amongst your tables don't seem to make any business sense of which I am familiar: Since you have "customer_id" as a foreign key on the rows of the "stock" table, this implies that "Each stock is for one (and only one) customer." Additionally, since the "company" table has a foreign key ("customer_id") to the "Customer" table, this implies: "Each company has one and only one customer." Don't these relationships seem bogus to you? If I have misinterpreted your data, please correct me by restating the correct meaning of your data.
Second, you have two data errors:
1) As your data structures imply above, "Each company has one and only one customer," yet you show Company C11 appearing under both Customer 111 and Customer 112. So, to produce the results in the format you want with the (very suspicious) data relationships you imply, then we must change Company C11 under Customer 112 to Company C15, for example.
2) Your data structure also implies, "Each stock is for one (and only one) customer." Yet, Stock S12 is for both Customer 111 and Customer 112. So, again, to make your data work, we shall change Stock S12 under Customer 112 to Stock S15.
Then lastly, because your data relationships are spurious, the results of data that reflect your format and content, above, result in "unholy"/spurious relationships:
Section 1 -- Pertinent contents of your data tables:
Code:
select * from customer;
CUSTOMER_ID
-----------
111
112
select * from company;
Company_ID CUSTOMER_ID
---------- -----------
c11 111
c12 111
c13 112
c15 112
c14 112
select * from stocks;
STOCK_ID CUSTOMER_ID
-------- -----------
s11 111
s12 111
s13 112
s14 112
s15 112
Section 2 -- SQL Query that "tries" to produce your results:
Code:
select cu.customer_id, co.company_id company, s.stock_id stock
from customer cu, company co, stocks s
where s.customer_id = cu.customer_id
and co.customer_id = cu.customer_id;
CUSTOMER_ID COMPANY STOCK
----------- ------- -----
111 c11 s11
111 c12 s11
111 c11 s12
111 c12 s12
112 c13 s13
112 c15 s13
112 c14 s13
112 c13 s14
112 c15 s14
112 c14 s14
112 c13 s15
112 c15 s15
112 c14 s15
So there is really no logical way to produce the results you want because your database design is faulty. If you design your tables to reflect your true business situation, then I am certain we can produce the results you want.
We can help you with your database design if you can specify/restate your true business-application relationships amongst your tables (business entities) in a way that makes business sense.
![[santa] [santa] [santa]](/data/assets/smilies/santa.gif)
Mufasa
(aka Dave of Sandy, Utah, USA)
[
Providing low-cost remote Database Admin services]
Click here to join
Utah Oracle Users Group on Tek-Tips if you use
Oracle in
Utah USA.