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

  • SEARCH
  • Home
  • 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 8587617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:36:41+00:00 2026-06-11T22:36:41+00:00

I have Parent + Reference tables where Reference table is as follows Ref_ID PARENT_ID

  • 0

I have Parent + Reference tables where
Reference table is as follows

Ref_ID    PARENT_ID    
-------------------
1           1            
2           1            
1           2       
3           2       
1           3       
3           3       
4           3       
2           4       
3           4       

Trying to return all distinct parent rows WHERE ref_id in ( 2, 3 )
using a join instead of using a subquery
but duplicates are being returned for parent via join query

Any help is appreciated

FYI – there are 4-7 tables in the query ( depending on user selections )so performance is a huge factor

  • 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-11T22:36:42+00:00Added an answer on June 11, 2026 at 10:36 pm

    Ok,

    First the code

    DECLARE @Temp TABLE(
    Ref_ID int,
    ParentID int
    )
    
    INSERT INTO @Temp VALUES (1 , 1)
    INSERT INTO @Temp VALUES (2 , 1)
    INSERT INTO @Temp VALUES (1 , 2)
    INSERT INTO @Temp VALUES (3 , 2)
    INSERT INTO @Temp VALUES (1 , 3)
    INSERT INTO @Temp VALUES (3 , 3)
    INSERT INTO @Temp VALUES (4 ,3)
    INSERT INTO @Temp VALUES (2 , 4)
    INSERT INTO @Temp VALUES (3 , 4)
    
    --First Example witout Ref_ID
    SELECT 
      DISTINCT ParentID
    FROM @Temp
    WHERE Ref_ID IN (2,3)
    
    --SECOND Example With Ref_ID
    SELECT 
       DISTINCT ParentID,
       Ref_ID
    FROM @Temp
    WHERE Ref_ID IN (2,3)
    

    The first query returns a distinct record of ParentID’s. It works fine because i haven’t requested for Ref_Id as well.

    Just to explain you when you requesting for Ref_ID as well, the query tries to return a distinct ParentID’s. But look at your records you have two ParentID’s which equals to 4
    But with Different Ref_ID so he returns them both. both of them matching the query rules
    enter image description here

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

Sidebar

Related Questions

I have a parent/child/grandchild type table relationship defined as follows: tableA: parentID description tableB:
Guys i currently have 2 tables that reference a lookup table. Ive been tasked
I have 2 tables. The parent table is learn_mor e and child table is
I have parent/child relationship set up via Node Reference. A Child record can have
I have a parent class which contains a child object. I am using set
I have a tree (nested categories) stored as follows: CREATE TABLE `category` ( `category_id`
I have a SQL 2005 table with millions of rows in it that is
I have a table (TableB) that has a foreign key relationship with a parent
I have some Oracle tables that represent a parent-child relationship. They look something like
I have a MySQL database holding hierarchical data using the Closure Table method. A

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.