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


Join creating two records


Join creating two records

Author
Message
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2233 Visits: 784
I have a join that is doing something that I can not explain.

I have two tables.. linking on Delivery No and Item No..

Temp table #Delivery has one record with Delivery No = 00835308 and the second table does not have that Delivery at all. But I get two records running...

select *
FROM #Delivery Left Outer JOIN
details_2 ON cast(#Delivery.Delivery as int) = cast(details_2.Delivery_Doc as int) AND
cast(#Delivery.Delivery_Item as int) = cast(details_2.Delivery_Item as int)
Where cast(delivery as int) = 83535308

Like gives only one record.. but when I run without the WHERE it never completes the INSERT.
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13096 Visits: 5478
dwilliscp (5/8/2013)
I have a join that is doing something that I can not explain.

I have two tables.. linking on Delivery No and Item No..

Temp table #Delivery has one record with Delivery No = 00835308 and the second table does not have that Delivery at all. But I get two records running...

select *
FROM #Delivery Left Outer JOIN
details_2 ON cast(#Delivery.Delivery as int) = cast(details_2.Delivery_Doc as int) AND
cast(#Delivery.Delivery_Item as int) = cast(details_2.Delivery_Item as int)
Where cast(delivery as int) = 83535308

Like gives only one record.. but when I run without the WHERE it never completes the INSERT.



Using LEFT JOIN means that as long as record(s) with "cast(delivery as int) = 83535308" exists in #Delivery, these records will be selected regardless of anything existing or not existing in your "details_2" table.
What do you mean by " I run without the WHERE it never completes the INSERT."?
If you remove your WHERE clause, all records from #Delivery will be selected.
If your "details_2" table contains more than one matching record for a single record in #Delivery, than you should expect the same row from #Delivery to be returned as many times as may matches found in "details_2"

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2233 Visits: 784
Eugene Elutin (5/8/2013)
dwilliscp (5/8/2013)
I have a join that is doing something that I can not explain.

I have two tables.. linking on Delivery No and Item No..

Temp table #Delivery has one record with Delivery No = 00835308 and the second table does not have that Delivery at all. But I get two records running...

select *
FROM #Delivery Left Outer JOIN
details_2 ON cast(#Delivery.Delivery as int) = cast(details_2.Delivery_Doc as int) AND
cast(#Delivery.Delivery_Item as int) = cast(details_2.Delivery_Item as int)
Where cast(delivery as int) = 83535308

Like gives only one record.. but when I run without the WHERE it never completes the INSERT.



Using LEFT JOIN means that as long as record(s) with "cast(delivery as int) = 83535308" exists in #Delivery, these records will be selected regardless of anything existing or not existing in your "details_2" table.
What do you mean by " I run without the WHERE it never completes the INSERT."?
If you remove your WHERE clause, all records from #Delivery will be selected.
If your "details_2" table contains more than one matching record for a single record in #Delivery, than you should expect the same row from #Delivery to be returned as many times as may matches found in "details_2"


Since the first table had one record with a delivery of 00835308 and the second had none, I would have expected the SQL to return one record. What I got was two records that are the same.

The code I have placed here.. shows the problem, but the WHERE was added to show why my query is failling with a PK failure.... and the Insert Into was removed.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65384 Visits: 17980
dwilliscp (5/8/2013)


Since the first table had one record with a delivery of 00835308 and the second had none, I would have expected the SQL to return one record. What I got was two records that are the same.

The code I have placed here.. shows the problem, but the WHERE was added to show why my query is failling with a PK failure.... and the Insert Into was removed.


Either the first or second table has two rows with that value based on the query you posted.

I would suggest that you change the datatype at least in your temp table to an int.


select *
FROM #Delivery Left Outer JOIN
details_2 ON cast(#Delivery.Delivery as int) = cast(details_2.Delivery_Doc as int) AND
cast(#Delivery.Delivery_Item as int) = cast(details_2.Delivery_Item as int)
Where cast(delivery as int) = 83535308


Based on that query every row has to have a value that is directly castable to an int. Save yourself the hassles and just change the datatype.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65384 Visits: 17980
What are the results of these two queries?


select COUNT(*) FROM #Delivery Where cast(delivery as int) = 83535308

select COUNT(*) from details_2 where cast(details_2.Delivery_Doc as int) = 83535308



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2233 Visits: 784
Sean Lange (5/8/2013)
What are the results of these two queries?


select COUNT(*) FROM #Delivery Where cast(delivery as int) = 83535308

select COUNT(*) from details_2 where cast(details_2.Delivery_Doc as int) = 83535308



One (Delivery) and zero (details_2)... that is what makes this so darn strange. If you do an inner join you come up with nothing.

I have found one other strange thing..
The outer join has data in the columns for details_2, so I do not understand how in the heck it is doing the link between the two tables... there shouldn't be anything on the details_2 side.
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2233 Visits: 784
Sean Lange (5/8/2013)
dwilliscp (5/8/2013)


Since the first table had one record with a delivery of 00835308 and the second had none, I would have expected the SQL to return one record. What I got was two records that are the same.

The code I have placed here.. shows the problem, but the WHERE was added to show why my query is failling with a PK failure.... and the Insert Into was removed.


Either the first or second table has two rows with that value based on the query you posted.

I would suggest that you change the datatype at least in your temp table to an int.


select *
FROM #Delivery Left Outer JOIN
details_2 ON cast(#Delivery.Delivery as int) = cast(details_2.Delivery_Doc as int) AND
cast(#Delivery.Delivery_Item as int) = cast(details_2.Delivery_Item as int)
Where cast(delivery as int) = 83535308


Based on that query every row has to have a value that is directly castable to an int. Save yourself the hassles and just change the datatype.


Folks want to see the same Delivery No as in our ERP software... with the leading zero's, so I am stuck with it. I did try and create two new columns PK_Delivery and PK_Item (casting these as int earlier in the process), but the result was just the same.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98991 Visits: 38998
dwilliscp (5/8/2013)
...
I have found one other strange thing..
The outer join has data in the columns for details_2, so I do not understand how in the heck it is doing the link between the two tables... there shouldn't be anything on the details_2 side.


Your query in the original post is SELECT * ..., this means you are going to see the all the columne from both tables used in the query.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2233 Visits: 784
Lynn Pettis (5/8/2013)
dwilliscp (5/8/2013)
...
I have found one other strange thing..
The outer join has data in the columns for details_2, so I do not understand how in the heck it is doing the link between the two tables... there shouldn't be anything on the details_2 side.


Your query in the original post is SELECT * ..., this means you are going to see the all the columne from both tables used in the query.


True, but since this Delivery only exists in one table I would expect the columns related to the Details_2 to be NULL, but they are not.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65384 Visits: 17980
dwilliscp (5/8/2013)
Lynn Pettis (5/8/2013)
dwilliscp (5/8/2013)
...
I have found one other strange thing..
The outer join has data in the columns for details_2, so I do not understand how in the heck it is doing the link between the two tables... there shouldn't be anything on the details_2 side.


Your query in the original post is SELECT * ..., this means you are going to see the all the columne from both tables used in the query.


True, but since this Delivery only exists in one table I would expect the columns related to the Details_2 to be NULL, but they are not.


That means that the join condition was met.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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