Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:58:03+00:00 2026-06-12T21:58:03+00:00

I have 3 tables. Table A contains the data that my query is going

  • 0

I have 3 tables.
Table A contains the data that my query is going to be based on. It contains id1.
Table B is my connecting table, it contains 2 columns that I’m concerned with: id1 & id2.

Table C is my lookup table, it contains id2 and ReasonName. I need the column ‘ReasonName’ and all the data from table A.

If I do the following SQL:

 SELECT NAME, 
       STARTDATE, 
       ENDDATE, 
       REASON, 
       ID1 
FROM   TABLEA 
       LEFT JOIN TABLEB 
              ON TABLEA.ID1 = TABLEB.ID1

I am then able to do a second join on to TableC, however this is where the problem lies.
There are multiple matches in TableC so therefore my totalrows increases. I need to avoid this.
See image for what TableC looks like:

enter image description here

If my SQL statement now looks like this:

 SELECT TABLEA.NAME, 
       TABLEA.STARTDATE, 
       TABLEA.ENDDATE, 
       TABLEA.REASON, 
       TABLEA.ID1, 
       TABLEC.REASONNAME 
FROM   TABLEA 
       LEFT JOIN TABLEB 
              ON TABLEA.ID1 = TABLEB.ID1 
       LEFT JOIN TABLEC 
              ON TABLEB.ID2 = TABLEC.ID2 

Then my rows increase by around 1000. This is because it is quite possible that TableA.id1 matches multiple id2’s found in TableC. It then duplicates rows apart from the different TableC.ReasonName.

In TableC there is one ReasonName that I am concerned with. For this example I will say it is ‘reasonf’.

What I need is to bring through ReasonName from TableC, BUT only the ones that contain reasonf, the rest I want to say NULL or to do a COALESCE with ‘No Reason’.
I have tried putting a WHERE clause into the statement. See SQL:

 SELECT TABLEA.NAME, 
       TABLEA.STARTDATE, 
       TABLEA.ENDDATE, 
       TABLEA.REASON, 
       TABLEA.ID1, 
       TABLEC.REASONNAME 
FROM   TABLEA 
       LEFT JOIN TABLEB 
              ON TABLEA.ID1 = TABLEB.ID1 
       LEFT JOIN TABLEC 
              ON TABLEB.ID2 = TABLEC.ID2 
WHERE  TABLEC.ID2 = 'asd1f5as98a4' 

But then it will only bring though those records where there is a match with ‘reasonf’, I want it to display reasonf people and ignore the rest (leave as null or something) so I have no duplicates but my full result set.

I’m thinking I may need to change to a right join or possibly change the WHERE but I’m not entirely sure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:58:04+00:00Added an answer on June 12, 2026 at 9:58 pm

    Move your WHERE clause into the join…

    select 
      TableA.name,
      TableA.startDate,
      TableA.endDate,
      TableA.Reason,
      TableA.id1,
      COALESCE(TableC.ReasonName, 'No Reason') AS FilteredReasonName
    from
      TableA
    left join
      TableB
        on  TableA.id1 = TableB.id1
        and TableB.id2 = 'asd1f5as98a4'
    left join
      TableC
        on  TableB.id2 = TableC.id2
    

    Now, TableB (the source of the duplication) only joins if it is ReasonF or not at all. Then the next join goes and looks up the ReasonName for that code.

    If nothing is found, the COALESCE() replaces the NULL with 'No Reason'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables & data like this: venues table contains : id +----+---------+ |
I have a table that contains all the data about users . Users can
I have two tables: a schedule table that contains information about how an employee
I have a DataTable object that contains all the data for a Table visualization.
Say I have a table or 2 that contains data that will never or
I have a query result set that contains data that I need to filter,
I have a series of tables that contain data I want to full text
I have a table which contains data about which node has been visited. It
Ok, I have a table which contains multiple rows. Each row contains some data
I have a table which contains TV Guide data. In a simplified form, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.