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. Can predicates in the WHERE clause affect the type of join

Can predicates in the WHERE clause affect the type of join

Scheduled Pinned Locked Moved Database
databasequestion
8 Posts 5 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.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Hello In the query below, since I am using a LEFT JOIN I would expect all rows from podetm to be returned. This is true as long as the line that is commented out is not included. As soon as I include a predicate from the joined table, I seem to get only rows that exist in stockm in the result set. Why?

    select * from scheme.podetm d
    left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
    where d.qty_received >0 and d.inv_value_posted =0
    --and s.analysis_a not in ( 'LNG', 'INFANTRUST')

    J A U 3 Replies Last reply
    0
    • R Richard Berry100

      Hello In the query below, since I am using a LEFT JOIN I would expect all rows from podetm to be returned. This is true as long as the line that is commented out is not included. As soon as I include a predicate from the joined table, I seem to get only rows that exist in stockm in the result set. Why?

      select * from scheme.podetm d
      left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
      where d.qty_received >0 and d.inv_value_posted =0
      --and s.analysis_a not in ( 'LNG', 'INFANTRUST')

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Yes, you're effectively turning it into a normal join in this case. Think about it, when you add the line and s.analysis_a not in ( 'LNG', 'INFANTRUST') you're telling it to give you all records where s.analysis_a does not contain 'LNG' or 'INFANTRUST'. You didn't tell it to give you records where s.analysis_a is null. So try this:

      select * from scheme.podetm d
      left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
      where d.qty_received >0 and d.inv_value_posted =0
      and (s.analysis_a not in ( 'LNG', 'INFANTRUST') OR s.analysis_a IS NULL)

      Wrong is evil and must be defeated. - Jeff Ello[^]

      P R 2 Replies Last reply
      0
      • J Jorgen Andersson

        Yes, you're effectively turning it into a normal join in this case. Think about it, when you add the line and s.analysis_a not in ( 'LNG', 'INFANTRUST') you're telling it to give you all records where s.analysis_a does not contain 'LNG' or 'INFANTRUST'. You didn't tell it to give you records where s.analysis_a is null. So try this:

        select * from scheme.podetm d
        left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
        where d.qty_received >0 and d.inv_value_posted =0
        and (s.analysis_a not in ( 'LNG', 'INFANTRUST') OR s.analysis_a IS NULL)

        Wrong is evil and must be defeated. - Jeff Ello[^]

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Looks like he did to me.

        J 1 Reply Last reply
        0
        • P PIEBALDconsult

          Looks like he did to me.

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          :confused:

          Wrong is evil and must be defeated. - Jeff Ello[^]

          P 1 Reply Last reply
          0
          • J Jorgen Andersson

            Yes, you're effectively turning it into a normal join in this case. Think about it, when you add the line and s.analysis_a not in ( 'LNG', 'INFANTRUST') you're telling it to give you all records where s.analysis_a does not contain 'LNG' or 'INFANTRUST'. You didn't tell it to give you records where s.analysis_a is null. So try this:

            select * from scheme.podetm d
            left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
            where d.qty_received >0 and d.inv_value_posted =0
            and (s.analysis_a not in ( 'LNG', 'INFANTRUST') OR s.analysis_a IS NULL)

            Wrong is evil and must be defeated. - Jeff Ello[^]

            R Offline
            R Offline
            Richard Berry100
            wrote on last edited by
            #5

            Jorgen, as usual... Thanks for the clear explanation :) Getting all the required rows after following your suggestion.

            1 Reply Last reply
            0
            • J Jorgen Andersson

              :confused:

              Wrong is evil and must be defeated. - Jeff Ello[^]

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              :doh: I was looking at yours. :sigh:

              1 Reply Last reply
              0
              • R Richard Berry100

                Hello In the query below, since I am using a LEFT JOIN I would expect all rows from podetm to be returned. This is true as long as the line that is commented out is not included. As soon as I include a predicate from the joined table, I seem to get only rows that exist in stockm in the result set. Why?

                select * from scheme.podetm d
                left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
                where d.qty_received >0 and d.inv_value_posted =0
                --and s.analysis_a not in ( 'LNG', 'INFANTRUST')

                A Offline
                A Offline
                Arora_Ankit
                wrote on last edited by
                #7

                If you want to include that condition then include that in the Join like left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product and s.analysis_a not in ( 'LNG', 'INFANTRUST') Else you will face that issue

                1 Reply Last reply
                0
                • R Richard Berry100

                  Hello In the query below, since I am using a LEFT JOIN I would expect all rows from podetm to be returned. This is true as long as the line that is commented out is not included. As soon as I include a predicate from the joined table, I seem to get only rows that exist in stockm in the result set. Why?

                  select * from scheme.podetm d
                  left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product
                  where d.qty_received >0 and d.inv_value_posted =0
                  --and s.analysis_a not in ( 'LNG', 'INFANTRUST')

                  U Offline
                  U Offline
                  User 11117574
                  wrote on last edited by
                  #8

                  try it: select * from scheme.podetm d left join scheme.stockm s on s.warehouse+s.product = d.warehouse+d.product where d.qty_received >0 and d.inv_value_posted =0 and (s.analysis_a not in ('LNG','INFANREUST') or s.analysis_a is null)

                  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