We help IT Professionals succeed at work.

Problem with simple LEFT JOIN in SQL Query

katiep23
katiep23 asked
on
508 Views
Last Modified: 2008-01-09
I have a left join that's not acting right.  My guess is I have made some stupid error, but I cannot tell.  First off, I have this initial query that produces 16 records:

SELECT a.IMITM, a.IMLITM, a.IMSTKT
    FROM F4101 a
            WHERE a.IMSTKT <> 'O'
            AND a.IMSTKT <> 'U'
            AND a.IMSRP6 = '20' AND a.IMSRP7 = '220' AND a.IMSRP0 = 'CX'
        ORDER BY a.IMLITM ASC

Now when I do a simple left join with another tables, I get 100 results!

with Branch As (SELECT IBITM, IBMULT, IBSAFE, IBROQI      FROM f4102)
    SELECT a.IMITM, a.IMSTKT, u.IBMULT, u.IBSAFE, u.IBROQI
    FROM F4101 a
      LEFT JOIN Branch u on TRIM(u.IBITM) = a.IMITM
            WHERE a.IMSTKT <> 'O'
            AND a.IMSTKT <> 'U'
            AND a.IMSRP6 = '20' AND a.IMSRP7 = '220' AND a.IMSRP0 = 'CX'
        ORDER BY a.IMLITM ASC

I have this query doing several other left joins with no problem; but this join in particular is giving me hell.  Any thoughts?  Thanks!
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Ugh, I'm so stupid.  I realized this right after I posted the question!
CERTIFIED EXPERT
Top Expert 2011

Commented:
so try it as

with Branch
   As (SELECT distinct trim(IBITM) as IBITM, IBMULT, IBSAFE, IBROQI    
            FROM   f4102)
    SELECT a.IMITM, a.IMSTKT, u.IBMULT, u.IBSAFE, u.IBROQI
       FROM F4101 a
        LEFT JOIN Branch u
           on u.IBITM = a.IMITM
    WHERE a.IMSTKT <> 'O'
         AND a.IMSTKT <> 'U'
         AND a.IMSRP6 = '20'
         AND a.IMSRP7 = '220'
         AND a.IMSRP0 = 'CX'
     ORDER BY a.IMLITM ASC

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.