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. LEFT OUTER JOIN not working

LEFT OUTER JOIN not working

Scheduled Pinned Locked Moved Database
helpquestionannouncement
6 Posts 4 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.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    I am trying to use a join to pull all 50 states from a table and data from another table if there is anything there. Here is the code

    SELECT S.szState, SUM(O.CAP_OFFLIN) AS fOffline
    FROM tblStates AS S LEFT OUTER JOIN tblOutages AS O ON S.szState = O.UNIT_STATE
    WHERE NOT(O.TA_START > '11/23/2011') AND NOT(O.TA_END < '11/23/2011')
    GROUP BY S.szState

    I cannot for the life of me get the 50 states to list. I have changed the join order (using tblOutages then tblStates with RIGHT JOIN) but cannot get the join to work. Anything stupid I am doing? tblStates is szState nvarchar(2) tblOutages is CAP_OFFLINE FLOAT, UNIT_STATE nvarchar(4) Thx Mark Jackson UPDATE - THE WHERE clause is causing the issue. Removing it causes all 50 states to show. I tried to put it in a HAVING clause but I can't do that unless I include it in the result set.

    B C M 3 Replies Last reply
    0
    • M mjackson11

      I am trying to use a join to pull all 50 states from a table and data from another table if there is anything there. Here is the code

      SELECT S.szState, SUM(O.CAP_OFFLIN) AS fOffline
      FROM tblStates AS S LEFT OUTER JOIN tblOutages AS O ON S.szState = O.UNIT_STATE
      WHERE NOT(O.TA_START > '11/23/2011') AND NOT(O.TA_END < '11/23/2011')
      GROUP BY S.szState

      I cannot for the life of me get the 50 states to list. I have changed the join order (using tblOutages then tblStates with RIGHT JOIN) but cannot get the join to work. Anything stupid I am doing? tblStates is szState nvarchar(2) tblOutages is CAP_OFFLINE FLOAT, UNIT_STATE nvarchar(4) Thx Mark Jackson UPDATE - THE WHERE clause is causing the issue. Removing it causes all 50 states to show. I tried to put it in a HAVING clause but I can't do that unless I include it in the result set.

      B Offline
      B Offline
      berba
      wrote on last edited by
      #2

      when you use GROUP BY you need to use and aggregate_function as well.

      C 1 Reply Last reply
      0
      • B berba

        when you use GROUP BY you need to use and aggregate_function as well.

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

        It looks like there is a sum operation, so whould that not be the aggregate function? :)

        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
        • M mjackson11

          I am trying to use a join to pull all 50 states from a table and data from another table if there is anything there. Here is the code

          SELECT S.szState, SUM(O.CAP_OFFLIN) AS fOffline
          FROM tblStates AS S LEFT OUTER JOIN tblOutages AS O ON S.szState = O.UNIT_STATE
          WHERE NOT(O.TA_START > '11/23/2011') AND NOT(O.TA_END < '11/23/2011')
          GROUP BY S.szState

          I cannot for the life of me get the 50 states to list. I have changed the join order (using tblOutages then tblStates with RIGHT JOIN) but cannot get the join to work. Anything stupid I am doing? tblStates is szState nvarchar(2) tblOutages is CAP_OFFLINE FLOAT, UNIT_STATE nvarchar(4) Thx Mark Jackson UPDATE - THE WHERE clause is causing the issue. Removing it causes all 50 states to show. I tried to put it in a HAVING clause but I can't do that unless I include it in the result set.

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

          Not sure, but you may also have to include in your where clause something that allows TA_START and TA_END to be NULL.

          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
          • M mjackson11

            I am trying to use a join to pull all 50 states from a table and data from another table if there is anything there. Here is the code

            SELECT S.szState, SUM(O.CAP_OFFLIN) AS fOffline
            FROM tblStates AS S LEFT OUTER JOIN tblOutages AS O ON S.szState = O.UNIT_STATE
            WHERE NOT(O.TA_START > '11/23/2011') AND NOT(O.TA_END < '11/23/2011')
            GROUP BY S.szState

            I cannot for the life of me get the 50 states to list. I have changed the join order (using tblOutages then tblStates with RIGHT JOIN) but cannot get the join to work. Anything stupid I am doing? tblStates is szState nvarchar(2) tblOutages is CAP_OFFLINE FLOAT, UNIT_STATE nvarchar(4) Thx Mark Jackson UPDATE - THE WHERE clause is causing the issue. Removing it causes all 50 states to show. I tried to put it in a HAVING clause but I can't do that unless I include it in the result set.

            M Offline
            M Offline
            mjackson11
            wrote on last edited by
            #5

            Changing the WHERE clause to be part of the JOIN clause produced the desired results

            SELECT S.szState, SUM(O.CAP_OFFLIN) AS fOffline
            FROM tblStates AS S LEFT JOIN tblOutages AS O ON S.szState = O.UNIT_STATE
            AND NOT(O.TA_START > '11/23/2011')
            AND NOT(O.TA_END < '11/23/2011')
            GROUP BY S.szState

            Thx Mark Jackson

            I 1 Reply Last reply
            0
            • M mjackson11

              Changing the WHERE clause to be part of the JOIN clause produced the desired results

              SELECT S.szState, SUM(O.CAP_OFFLIN) AS fOffline
              FROM tblStates AS S LEFT JOIN tblOutages AS O ON S.szState = O.UNIT_STATE
              AND NOT(O.TA_START > '11/23/2011')
              AND NOT(O.TA_END < '11/23/2011')
              GROUP BY S.szState

              Thx Mark Jackson

              I Offline
              I Offline
              i j russell
              wrote on last edited by
              #6

              The WHERE clause was causing the LEFT OUTER JOIN to act as an INNER JOIN, hence you lost all of the records where there wasn't a match. Well done on finding the right way to do it and for showing others what you did!

              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