sql - SELECT inner joined columns where only one column can not have duplicate record -
i want select inner joined tables, , if there duplicate record in 'column d', not display entire inner joined row.
table 1
a b 1 car 1 boat 1 man
table 2
c d 1 dog *dog duplicate, display once. 1 dog 1 cat
here inner joined sql select statement far:
select distinct b c d table1 inner join table2 on table1.a = table2.c <duplicate> not in result
result should be:
1 car 1 dog *1 boat 1 dog* <--dog duplicate should not displayed 1 man 1 cat
it sounds want arbitrary pairs drawn 2 tables, long second column not have repeated values. whether car/dog or boat/dog unspecified, don't want both.
declare @table1 table ( id1 int, value1 varchar(8) ) insert @table1 ( id1, value1 ) values ( 1, 'car' ) insert @table1 ( id1, value1 ) values ( 1, 'boat' ) insert @table1 ( id1, value1 ) values ( 1, 'man' ) declare @table2 table ( id2 int, value2 varchar(8) ) insert @table2 ( id2, value2 ) values ( 1, 'dog' ) insert @table2 ( id2, value2 ) values ( 1, 'dog' ) insert @table2 ( id2, value2 ) values ( 1, 'cat' ) -- combinations. select value1, value2, row_number() on ( order value2 ) 'rownumber' @table1 l inner join @table2 r on l.id1 = r.id2 -- combinations in values in second column not recur. select value1, value2 ( select value1, value2, row_number() on ( order value2 ) 'rownumber' @table1 l inner join @table2 r on l.id1 = r.id2 ) blue rownumber = ( select min( rownumber ) ( select value1, value2, row_number() on ( order value2 ) 'rownumber' @table1 l inner join @table2 r on l.id1 = r.id2 ) grey value2 = blue.value2 )
Comments
Post a Comment