944,118 Members | Top Members by Rank

Ad:
  • Oracle Discussion Thread
  • Marked Solved
  • Views: 7055
  • Oracle RSS
Feb 1st, 2007
0

Cartesian Product Problem

Expand Post »
Hi everyone,

I am still fairly new to Oracle and SQL. I have two tables with the same information in them. Table1 has the same columns as table2. The differnce between them is the date column. Table1 has 2005 records while table2 has 2006 orders. I am trying to write a SQL statement that will display the contents of these tables across. Meaning, I have all columns from table1 on the left and then immediately right of that, I have the elements from table2. I want it like this so I can look at the 2005 records and the 2006 records going right across the row so that I can compare them. Below is an example of the statement I am trying to use:

Oracle Syntax (Toggle Plain Text)
  1. SELECT a.DATE,
  2. a.qty,
  3. a.amt,
  4. b.DATE AS Date2
  5. b.qty AS Quantity,
  6. b.amt AS Amount
  7. FROM table1 a, table2 b;

I want to have the result table look like this:

date | qty | amt | date2 | Quantity | Amount

The columns going straight across.


I keep getting warnings that this gives me a cartesian product. Table1 and Table2 each have roughly 50,000 rows in them, so if this is a cartesian join, this is a huge problem. Can anyone help me accomplish this?

Thanks,

Nick
Similar Threads
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Feb 7th, 2007
0

Re: Cartesian Product Problem

Your problem is that you are not specifying a column to join on.

What do the two tables have in common? What is it about a record in table A that means it should be placed next to a record in table B?

Usually on a join you would say something like

select pos.* 
from person per
join posession pos on pos.person_id = per.id
where per.id = 5

By not specifying what to join it links every tuple in table A to every one in table B, so you have A*B tuples returned.

If there is nothing in common between the tables (as I suspect if they are identical) the correct way of displaying the data would be to use a UNION.

Oracle Syntax (Toggle Plain Text)
  1. SELECT * FROM tableA
  2. UNION
  3. SELECT * FROM tableB

This will give you A+B records back
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Feb 7th, 2007
0

Re: Cartesian Product Problem

Thank you!
I tried joining it on something and it worked!
Again, thank you!
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Feb 16th, 2007
0

Prabu:

how many column can we add in a table ? i mean maximum no of column in a table?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g.prabu is offline Offline
7 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Oracle Forum Timeline: order and then compare values from same column
Next Thread in Oracle Forum Timeline: maximum no of column in a table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC