SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


inner join returns cartesian product why?


inner join returns cartesian product why?

Author
Message
Michael Tocik
Michael Tocik
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4576 Visits: 172
Hi,
I have two order tables that I am trying to join. Both have have 13 records in them but when I do an inner join on the order number I get 169 records. Both tables have a data type of varchar(7) Here is the join;

SELECT INVOICE_DETAIL_CONF_ALL.order_number
FROM INVOICE_DETAIL_CONF_ALL
inner join ORDER_EXTRACT_AUTO
on [Ord No] = ORDER_NUMBER


What am I missing?
tommyh
tommyh
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3426 Visits: 2000
Michael Tocik (3/7/2011)
Hi,
I have two order tables that I am trying to join. Both have have 13 records in them but when I do an inner join on the order number I get 169 records. Both tables have a data type of varchar(7) Here is the join;

SELECT INVOICE_DETAIL_CONF_ALL.order_number
FROM INVOICE_DETAIL_CONF_ALL
inner join ORDER_EXTRACT_AUTO
on [Ord No] = ORDER_NUMBER


What am I missing?


Apart from some DDL code to see what the tables look like?

So just guessing wild:
- All the rows in both tables actually have the same Order_number

- You joining on 2 columns from the same table. Try and add the table name before the column.

/T
Michael Tocik
Michael Tocik
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4576 Visits: 172
dope!Blush

My mistake....I was joining on the wrong field
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72947 Visits: 20314
Michael Tocik (3/8/2011)
dope!Blush

My mistake....I was joining on the wrong field


Easily done. Try to get into the habit of using table aliases:

SELECT i.order_number
FROM INVOICE_DETAIL_CONF_ALL i
INNER JOIN ORDER_EXTRACT_AUTO o
ON o.[Ord No] = i.ORDER_NUMBER

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search