Join creating two records

  • 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.

  • 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[/url]

  • 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.

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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.

  • 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.

  • 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 (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.

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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.

    So, if the columns being displayed from details_2 are not NULL, what values are displayed in details_2.Delivery_Doc and details_2.Delivery_Item ?

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • dwilliscp (5/8/2013)


    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.

    Based on what you have in result of tqo COUNT queries your posted query of

    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

    will return one row only.

    So, I guess you posted not exact query you are running (probably you have omitted some things which you might think were insignificant).

    _____________________________________________
    "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[/url]

  • The only things I left out was the listing of columns and the complete name (liked server.database.dbo.table), and the WHERE includes a limit on the #delivery... "Where #Delivery.Load_From = 'Something' ".

    I am not sure why I did not find this yesterday... but when I run the following query today I find two records:

    select *

    from details_2

    where cast(delivery_doc as int) = 83535308

    so now I get to find out why there are two records here .. that look the same.

  • dwilliscp (5/9/2013)


    The only things I left out was the listing of columns and the complete name (liked server.database.dbo.table), and the WHERE includes a limit on the #delivery... "Where #Delivery.Load_From = 'Something' ".

    I am not sure why I did not find this yesterday... but when I run the following query today I find two records:

    select *

    from details_2

    where cast(delivery_doc as int) = 83535308

    so now I get to find out why there are two records here .. that look the same.

    Do you have more than one schema in your database? If there are tables named [details_2] in more than one schema, the fact that you have used an unqualified table name means that SQL Server may be resolving the table name to a schema other than the one you expect.

    Jason Wolfkill

  • Most, but not all, are dbo... but this table exists only in dbo. I think what I might have done was not to cast delivery in the where statement... the first day... at least that is the only thing I can think would explain the null record set when I was looking for where I expected problem to be.

  • Viewing 15 posts - 1 through 14 (of 14 total)

    You must be logged in to reply to this topic. Login to reply