1

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    
2
  • Please provide sample data and desired output. Aug 14, 2012 at 8:14
  • You have to tell the server, explicitly, how it should select values for the other columns for each distinct B.Val3 value. It won't guess, it has to be told. To get decent answers here, you need to tell us what the rule(s) are too. Aug 14, 2012 at 8:33

2 Answers 2

6

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 
7
  • In order to understand JohnLBevan's (fine) answer you may want to learn about group-by clauses. Maybe this stackoverflow.com/questions/11743219/… helps
    – jos
    Aug 14, 2012 at 8:21
  • Thanks Jos :). Another option I just thought of is Data Shaping - i.e. a way to get the distinct b.v3 values in a "header", with the other values as details/children. That's only possible if you're using ADO, but including in the comments for completeness: 4guysfromrolla.com/webtech/092599-1.shtml
    – JohnLBevan
    Aug 14, 2012 at 8:23
  • no, val3 in the table B not unique. but val1 in all tables(A, B, C) are unique and primary key
    – hamed aj
    Aug 14, 2012 at 8:28
  • thanks JohnLBevan, i use first solution and it work properly. do you think this solution is well and optimal?
    – hamed aj
    Aug 14, 2012 at 8:31
  • No worries. The query should be pretty efficient; without knowing your table structure / indexes I can't say for sure if it's optimal, but give it a go on a large data set and see how long it takes / take a look at the query plan to see what indexes it's using. Depending on your requirements you may also want to tweak the order by clause to change which of the possible results for each b.v3 value to select.
    – JohnLBevan
    Aug 14, 2012 at 9:03
0

It looks like you are taking the rows from B, with the first match in A and then any match in C.

You can do this as:

select *
from B join
     (select A.*, row_number() over (partition by A.val2 order by A.val1) as seqnum
      from A
     ) A
     on B.val1 = A.val2 and
        A.seqnum = 1 join
     C
     on A.val3 = C.val1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.