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 8387283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:10:12+00:00 2026-06-09T18:10:12+00:00

I have 3 tables that join with together and i want select distinct rows

  • 0

I have 3 tables that join with together and i want select distinct rows on the B.val3. i use something like this :

SELECT A.val1,A.val2, B.val1, B.val2, B.val3, C.val1, C.val2
FROM A INNER JOIN
B ON A.val1 = B.val1 INNER JOIN
C ON A.val1 = C.val1

this statement get to me 24 rows with duplicate B.val3
now, when i use SELECT DISTINCT A.val1,A.val2, B.val1,..., all 24 records are retrieved, but i want only distinct rows on the B.val3 retrieved (8 rows)

when i use something like :

SELECT DISTINCT A.val1,A.val2, B.val1,
...
C ON A.val1 = C.val1 GROUP BY B.val3

i received an error :

Msg 8120, Level 16, State 1, Line 13
Column 'A.val1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

EDIT : structure and data

table A as follow : val2 is foreign key of B and val3 is foreign key C

val1   val2    val3
-----  -----   -----
1       100     200
2       100     201
3       101     200
4       102     200
5       102     201

table B :

val1   val2    val3
-----  -----   -----
100     a2      aaa
101     b2      bbb
102     c2      ccc

table C :

val1   val2    
-----  -----   
200     a3      
201     b3      

and i want get something like :

A.val1  A.val2  A.val3  B.val1  B.val2  B.val3  C.val1  C.val2
-----   -----   -----   -----   -----   -----   -----   -----
1       100      200     100      a2     aaa      200    a3
3       101      200     101      b2     bbb      200    a3      
4       102      201     102      c2     ccc      201    b3    
  • 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-09T18:10:13+00:00Added an answer on June 9, 2026 at 6:10 pm

    You could try something like this:

        select * 
        from 
        (
        SELECT A.val1,A.val2, B.val1, B.val2, B.val3, C.val1, C.val2
                , row_number() over 
                (
                    partition by b.val3 
                    order by A.val1, A.val2, B.val1, B.val2, C.val1, C.val2
                ) r
        FROM A 
        INNER JOIN B ON A.val1 = B.val1 
        INNER JOIN C ON A.val1 = C.val1
        ) x
        where x.r = 1
    

    or

        SELECT max(A.val1)
        ,max(A.val2)
        , max(B.val1)
        , max(B.val2)
        , B.val3
        , max(C.val1)
        , max(C.val2)
        FROM A 
        INNER JOIN B ON A.val1 = B.val1 
        INNER JOIN C ON A.val1 = C.val1
        group by b.val3
    

    Depending on what you’re trying to achieve. If those don’t do what you’re after, please can you provide more info on what you’re hoping to do / example data?

    The issue you have is when selecting a distinct b.val3 there may be multiple records associated:

    • Are values in column V3 in table B unique?
    • Are values in column V1 in tables A and/or C unique?

    If the answers to either of the above questions are no, you need to give SQL a way to decide which of the multiple possible records/results to select when choosing what data to display for the other columns.


    EDIT

    Based on example data given above, please find a script to replicate the sample info & display the solution:

    if object_id('a') is not null drop table a
    if object_id('b') is not null drop table b
    if object_id('c') is not null drop table c
    go
    create table b 
    (
        val1 int not null identity(100,1) primary key clustered
        , val2 nvarchar(2) not null
        , val3 nvarchar(3) not null
    )
    go
    create table c
    (
        val1 int not null identity(200,1) primary key clustered
        , val2 nvarchar(2) not null
    )
    go
    create table a
    (
        val1 int not null identity(1,1) primary key clustered
        , val2 int not null constraint fk_a_b foreign key references b(val1)
        , val3 int not null constraint fk_a_c foreign key references c(val1)
    )
    go
    
    --ids 100 - 105
    insert b
    select 'a2', 'aaa'
    union all select 'b2', 'bbb'
    union all select 'c2', 'ccc'
    union all select 'c3', 'ccc' --val3 is not unique
    union all select 'c4', 'ccc' --
    union all select 'b3', 'bbb' --
    
    --ids 200 - 204
    insert c
    select 'a3'
    union all select 'b3'
    union all select 'c3'
    union all select 'd3'
    union all select 'e3'
    
    insert a
    select 100, 200
    union all select 100, 200
    union all select 100, 201
    union all select 101, 200
    union all select 102, 200
    union all select 102, 201
    union all select 103, 201
    union all select 104, 201
    union all select 105, 201
    union all select 105, 202
    union all select 105, 203
    union all select 105, 204
    
    --what does the full result set look like?
    SELECT  A.val1 aval1
    ,       A.val2 aval2
    ,       B.val1 bval1
    ,       B.val2 bval2
    ,       B.val3 bval3
    ,       C.val1 cval1
    ,       C.val2 cval2  
    FROM A 
    INNER JOIN B 
        ON A.val2 = B.val1 
    INNER JOIN C 
        ON A.val3 = C.val1 
    
    --now show unique B's
    select Aval1, Aval2, Bval1, Bval2, Bval3, Cval1, Cval2     
    from      
    (     
        SELECT  A.val1 aval1
        ,       A.val2 aval2
        ,       B.val1 bval1
        ,       B.val2 bval2
        ,       B.val3 bval3
        ,       C.val1 cval1
        ,       C.val2 cval2       
        , row_number() over              
        (                 
            partition by b.val3                  
            order by b.val1, c.val1 --try playing with this to see how the results change / see what fits your requirements           
        ) r     
        FROM A 
        INNER JOIN B 
            ON A.val2 = B.val1 
        INNER JOIN C 
            ON A.val3 = C.val1    
    ) x     
    where x.r = 1 
    
    --what wasn't included in the unique B result set, but was in the full set?
    select Aval1, Aval2, Bval1, Bval2, Bval3, Cval1, Cval2     
    from      
    (     
        SELECT  A.val1 aval1
        ,       A.val2 aval2
        ,       B.val1 bval1
        ,       B.val2 bval2
        ,       B.val3 bval3
        ,       C.val1 cval1
        ,       C.val2 cval2       
        , row_number() over              
        (                 
            partition by b.val3                  
            order by b.val1, c.val1 --try playing with this to see how the results change / see what fits your requirements           
        ) r     
        FROM A 
        INNER JOIN B 
            ON A.val2 = B.val1 
        INNER JOIN C 
            ON A.val3 = C.val1    
    ) x     
    where x.r > 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I want to join together. Table1 Year, ID, Theme,
I have a 3 tables that I'm trying to join and get distinct results.
I have 4 different tables that I want to join. The tables are structured
I have a mysql problem. I have two tables like this that I need
I'd like a query that at its simplest will join 2 tables together with
I have 3 tables that I need to join, these join together fine using
I want to join two tables together and have ONLY the data in Table
I have two tables that I am outer joining together: Post left outer join
I have two tables that needs to be linked together, I want to count
I have two tables, Foo and Bar, I'd like to join them together so

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.