Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Join creating two records Expand / Collapse
Author
Message
Posted Wednesday, May 8, 2013 8:59 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:59 PM
Points: 315, Visits: 548
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.
Post #1450629
Posted Wednesday, May 8, 2013 9:06 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1450631
Posted Wednesday, May 8, 2013 9:13 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:59 PM
Points: 315, Visits: 548
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.
Post #1450640
Posted Wednesday, May 8, 2013 9:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:14 PM
Points: 13,126, Visits: 11,964
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 Moden's 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)
Post #1450648
Posted Wednesday, May 8, 2013 9:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:14 PM
Points: 13,126, Visits: 11,964
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 Moden's 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)
Post #1450649
Posted Wednesday, May 8, 2013 11:40 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:59 PM
Points: 315, Visits: 548
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.
Post #1450715
Posted Wednesday, May 8, 2013 11:41 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:59 PM
Points: 315, Visits: 548
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.
Post #1450716
Posted Wednesday, May 8, 2013 11:50 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:35 PM
Points: 23,064, Visits: 31,593
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.



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)
Post #1450723
Posted Wednesday, May 8, 2013 12:05 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:59 PM
Points: 315, Visits: 548
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.
Post #1450731
Posted Wednesday, May 8, 2013 12:23 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:14 PM
Points: 13,126, Visits: 11,964
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 Moden's 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)
Post #1450737
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse