Forum Discussion

bill7191's avatar
bill7191
New Member
4 years ago
Solved

Trouble getting SQL left join to Execute

Hi-- I'm trying to join two tables using SQL code in PQ. joining on a common field ID_NUM.  I was getting errors that the ID_NUM field was ambiguous, so I specifically defined the first with its tabl...
  • BA_Pete's avatar
    4 years ago

    Hi bill7191 ,

     

    I think the code that you've provided would actually create an INNER JOIN as you've put the WHERE clause outside the join, rather than adding it as an 'AND' in the ON clause. I think you should actually write it like this:

    SELECT
        a.ID_NUM,
        b.YR_CDE,
        b.TRM_CDE,
        b.CRS_CDE,
        b.REQUEST_NUM,
        b.GRADE_SCALE_CDE,
        b.CREDIT_TYPE_CDE,
        b.CREDIT_HRS,
        b.CRS_TITLE
    FROM
        STUDENT_CRS_HIST a
        LEFT OUTER JOIN STUDENT_DIV_MAST b
        ON a.ID_NUM = b.ID_NUM AND b.YR_CDE = '2021'

     

    But this isn't important, then next bit is: I'd recommend to scrap your native query all together.

    Bring both tableA and tableB into Power Query and filter them as required (b.YR_CDE = 2021 etc.) then perform the merge in PQ, which will fold to your source i.e. will send the optimised SQL that you're trying to create to the source for you.

    Pros:

    - As you're new to SQL, you won't actually need to write any code

    - You'll be able to quickly and easily visualise the source tables and the post-join table to ensure the correct results

    - PQ will fold the query to the SQL source, so absolutely no drop in perfomance

    - The ability to fold further transformations to the source will remain available.

    Cons:

    - NONE

     

    Pete