Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Trouble with LEFT JOIN SQL Statement

Trouble with LEFT JOIN SQL Statement

Scheduled Pinned Locked Moved Database
databasehelpsql-serversysadmincryptography
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominick Marciano
    wrote on last edited by
    #1

    Current we have a database that has two tables "order status" and "lead_free_certs". For each record in "order status" there may be 0 to many records in the "lead_free_certs" table. I'm trying to load a specific record from "order status" and if there is a matching record in "lead_free_certs" (based on project number), the most recent record in "lead_free_certs" based on the certificate number. I'm using VS2008 and I'm connecting to 2005 SQL Server. The SQL statement I have is:

    SELECT * FROM [Order Status] LEFT JOIN Lead_Free_Certs ON [Order Status].[Project #] = Lead_Free_Certs.[Project Num] WHERE ([Order Status].[Project #] = " & projectnumber & ") ORDER BY Lead_Free_Certs.[Cert Num] DESC

    The "projectnumber" variable is passed as an argument to the function. When I run this command no rows are returned. However if I want to load records only from the "order status" table then the following SQL statement works:

    "SELECT * FROM [Order Status] WHERE [Project #] = " & projectnumber

    So I know it is a problem with my LEFT JOIN statement. Any help would be greatly appreciated since this is the first time I'm trying to use JOIN statements.

    C 1 Reply Last reply
    0
    • D Dominick Marciano

      Current we have a database that has two tables "order status" and "lead_free_certs". For each record in "order status" there may be 0 to many records in the "lead_free_certs" table. I'm trying to load a specific record from "order status" and if there is a matching record in "lead_free_certs" (based on project number), the most recent record in "lead_free_certs" based on the certificate number. I'm using VS2008 and I'm connecting to 2005 SQL Server. The SQL statement I have is:

      SELECT * FROM [Order Status] LEFT JOIN Lead_Free_Certs ON [Order Status].[Project #] = Lead_Free_Certs.[Project Num] WHERE ([Order Status].[Project #] = " & projectnumber & ") ORDER BY Lead_Free_Certs.[Cert Num] DESC

      The "projectnumber" variable is passed as an argument to the function. When I run this command no rows are returned. However if I want to load records only from the "order status" table then the following SQL statement works:

      "SELECT * FROM [Order Status] WHERE [Project #] = " & projectnumber

      So I know it is a problem with my LEFT JOIN statement. Any help would be greatly appreciated since this is the first time I'm trying to use JOIN statements.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      Just a guess, but is it possible that the two columns of "Project #" and "Project Num" are different types and some implicit cast causes the JOIN to fail. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      D 2 Replies Last reply
      0
      • C Chris Meech

        Just a guess, but is it possible that the two columns of "Project #" and "Project Num" are different types and some implicit cast causes the JOIN to fail. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        D Offline
        D Offline
        Dominick Marciano
        wrote on last edited by
        #3

        Didn't think of that; yes they are different types. Project Num is nvarchar(255) and Project # is int. Would it be better to convert them to the same type or is there a way to do the conversion in the SQL statement. There might be a problem with the conversion because some of the records in Lead_Free_Certs table are not strictly numerical. Some of the numbers have hyphens or slashes in them.

        C S 2 Replies Last reply
        0
        • C Chris Meech

          Just a guess, but is it possible that the two columns of "Project #" and "Project Num" are different types and some implicit cast causes the JOIN to fail. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          D Offline
          D Offline
          Dominick Marciano
          wrote on last edited by
          #4

          Okay I tried casting and it worked. The new statement that works is:

          SELECT * FROM [Order Status] LEFT JOIN Lead_Free_Certs ON (CAST([Order Status].[Project #] AS nvarchar(255)) = Lead_Free_Certs.[Project Num]) WHERE ([Order Status].[Project #] = " & projectnumber & ") ORDER BY Lead_Free_Certs.[Cert Num] DESC

          Thanks for the help!

          C 1 Reply Last reply
          0
          • D Dominick Marciano

            Didn't think of that; yes they are different types. Project Num is nvarchar(255) and Project # is int. Would it be better to convert them to the same type or is there a way to do the conversion in the SQL statement. There might be a problem with the conversion because some of the records in Lead_Free_Certs table are not strictly numerical. Some of the numbers have hyphens or slashes in them.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            Add an int column called Project # to the Lead_Free_Certs table and figure out how to populate it based upon the value from the Project Num column. But it sounds like you are trying to join two tables that were'nt to be joined together. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            1 Reply Last reply
            0
            • D Dominick Marciano

              Okay I tried casting and it worked. The new statement that works is:

              SELECT * FROM [Order Status] LEFT JOIN Lead_Free_Certs ON (CAST([Order Status].[Project #] AS nvarchar(255)) = Lead_Free_Certs.[Project Num]) WHERE ([Order Status].[Project #] = " & projectnumber & ") ORDER BY Lead_Free_Certs.[Cert Num] DESC

              Thanks for the help!

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              You're welcome. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              1 Reply Last reply
              0
              • D Dominick Marciano

                Didn't think of that; yes they are different types. Project Num is nvarchar(255) and Project # is int. Would it be better to convert them to the same type or is there a way to do the conversion in the SQL statement. There might be a problem with the conversion because some of the records in Lead_Free_Certs table are not strictly numerical. Some of the numbers have hyphens or slashes in them.

                S Offline
                S Offline
                suddip
                wrote on last edited by
                #7

                The LEFT OUTER JOIN or simply LEFT JOIN return all rows from the first table listed after the FROM clause or left of JOIN keyword , no matter if they have matches in the right table of the JOIN keyword. Syntax : FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name ; Check here lot of examples : http://www.w3resource.com/sql/joins/perform-a-left-join.php

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups