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. TSQL - JOIN cancels out my date filter

TSQL - JOIN cancels out my date filter

Scheduled Pinned Locked Moved Database
salesphpdatabasesql-servertesting
4 Posts 3 Posters 16 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    Well today was a crappy day, thinking I can hit and run this small web page and clean it up. I spent hours modeling this in SQL Manager finally getting it to work, and then used it my PHP code, it worked but fell apart. Maybe I have it backwards, and I should call "Project" first, and then join "commission_Summary" The first example works, but when I join the "project" table, it cancels out my "commission_summary.startup_check_date" filter. I get that or think that my JOIN is wrong, or that my JOIN is calling up all records and overriding my date filter. I tried different types of JOINs, but with no success.

    SELECT
    commission_summary.project_no,
    commission_summary.EC,
    commission_summary.TC,
    CONVERT(CHAR(10), commission_summary.startup_check_date, 120) AS StartDate,
    CONVERT(CHAR(10), commission_summary.finished_check_date, 120) AS StopDate
    FROM commission_summary
    WHERE CAST(commission_summary.startup_check_date AS SMALLDATETIME) >= CAST('2022-01-01 00:00:00' AS SMALLDATETIME)
    ORDER BY commission_summary.startup_check_date DESC

    I tried adding one join at a time, and it worked or produced my desired results using just JOIN. I don't understand why this returns all the records, well I know it's JOIN related, just not sure why. My theory, is that the "Project" JOIN may have a duplicate column name. I wonder if I should of used a wildcard for testing, like SELECT * FROM. This returns all the records

    SELECT
    commission_summary.project_no,
    commission_summary.EC,
    commission_summary.TC,
    CONVERT(CHAR(10), commission_summary.startup_check_date, 120) AS StartDate,
    CONVERT(CHAR(10), commission_summary.finished_check_date, 120) AS StopDate,
    project.Status,
    project.Sales_no,
    project.swan_job,
    employee.Employee_ID,
    employee.FName,
    employee.LName,
    customer.Customer_no, customer.LName,
    customer.FName,
    customer.City
    FROM commission_summary
    JOIN project on commission_summary.project_no = project.project_no
    JOIN employee on commission_summary.employee_id = employee.Employee_Id
    JOIN customer on customer.Customer_No = project.Customer_no
    WHERE CAST(commission_summary.startup_check_date AS SMALLDATETIME) >= CAST('2022-01-01 00:00:00' AS SMALLDATETIME)
    AND project.status = 'construction' OR project.status = 'finished'
    ORDER BY commission_summary.startup_check_date DESC

    I'm confused by how this works, because it produces my desired results, of about 12 records in 2022.

    SELECT
    commissi

    G 1 Reply Last reply
    0
    • J jkirkerx

      Well today was a crappy day, thinking I can hit and run this small web page and clean it up. I spent hours modeling this in SQL Manager finally getting it to work, and then used it my PHP code, it worked but fell apart. Maybe I have it backwards, and I should call "Project" first, and then join "commission_Summary" The first example works, but when I join the "project" table, it cancels out my "commission_summary.startup_check_date" filter. I get that or think that my JOIN is wrong, or that my JOIN is calling up all records and overriding my date filter. I tried different types of JOINs, but with no success.

      SELECT
      commission_summary.project_no,
      commission_summary.EC,
      commission_summary.TC,
      CONVERT(CHAR(10), commission_summary.startup_check_date, 120) AS StartDate,
      CONVERT(CHAR(10), commission_summary.finished_check_date, 120) AS StopDate
      FROM commission_summary
      WHERE CAST(commission_summary.startup_check_date AS SMALLDATETIME) >= CAST('2022-01-01 00:00:00' AS SMALLDATETIME)
      ORDER BY commission_summary.startup_check_date DESC

      I tried adding one join at a time, and it worked or produced my desired results using just JOIN. I don't understand why this returns all the records, well I know it's JOIN related, just not sure why. My theory, is that the "Project" JOIN may have a duplicate column name. I wonder if I should of used a wildcard for testing, like SELECT * FROM. This returns all the records

      SELECT
      commission_summary.project_no,
      commission_summary.EC,
      commission_summary.TC,
      CONVERT(CHAR(10), commission_summary.startup_check_date, 120) AS StartDate,
      CONVERT(CHAR(10), commission_summary.finished_check_date, 120) AS StopDate,
      project.Status,
      project.Sales_no,
      project.swan_job,
      employee.Employee_ID,
      employee.FName,
      employee.LName,
      customer.Customer_no, customer.LName,
      customer.FName,
      customer.City
      FROM commission_summary
      JOIN project on commission_summary.project_no = project.project_no
      JOIN employee on commission_summary.employee_id = employee.Employee_Id
      JOIN customer on customer.Customer_No = project.Customer_no
      WHERE CAST(commission_summary.startup_check_date AS SMALLDATETIME) >= CAST('2022-01-01 00:00:00' AS SMALLDATETIME)
      AND project.status = 'construction' OR project.status = 'finished'
      ORDER BY commission_summary.startup_check_date DESC

      I'm confused by how this works, because it produces my desired results, of about 12 records in 2022.

      SELECT
      commissi

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      You have an OR in there without any parentheses, that might be the problem. Try this:

      AND (project.status = 'construction' OR project.status = 'finished')

      Both sides of the OR are using the same table column, so you could use IN instead

      AND project.status IN ('construction', 'finished')

      J J 2 Replies Last reply
      0
      • G Graham Breach

        You have an OR in there without any parentheses, that might be the problem. Try this:

        AND (project.status = 'construction' OR project.status = 'finished')

        Both sides of the OR are using the same table column, so you could use IN instead

        AND project.status IN ('construction', 'finished')

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        That seems to be it. Wow! I didn't catch that yesterday. Thanks

        If it ain't broke don't fix it Discover my world at jkirkerx.com

        1 Reply Last reply
        0
        • G Graham Breach

          You have an OR in there without any parentheses, that might be the problem. Try this:

          AND (project.status = 'construction' OR project.status = 'finished')

          Both sides of the OR are using the same table column, so you could use IN instead

          AND project.status IN ('construction', 'finished')

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          I always use parens. Removes the question for me an future maintenance programmers as to what I thought the precedence was.

          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