4

Given these two tables Table A1 has two rows with the same value 'a'

A1
a
a

Table A2 has two rows with primary key value A,B and they are associated with 'a'

A2
PK col2
A   a
B   a

What I want is a join of A1 and A2 with this result

a A
a B

Obviously inner join doesn't work here. Is there a way to do this in SQL Server 2008?

user714002
  • 59
  • 1
  • 1
  • 4

5 Answers5

9

You can wipe out the duplicates by using DISTINCT

select distinct
  A1.col1,
  A2.PK
from
  A1
  inner join A2
          on A1.col1 = A2.col2
njr101
  • 9,381
  • 7
  • 37
  • 55
1

If distinct is not restricted

SELECT DISTINCT a.*, b.pk
FROM    A1 a
INNER JOIN A2 b ON (a.[test] = b.fk)
TheVillageIdiot
  • 38,965
  • 20
  • 129
  • 186
0

There are no joining condition in the post, so we need to go for cross join. I have applied cross join and restrict the duplicate values using distinct.

Select distinct A1.Col1, A2.Pk
From A1 ,A2 
Romil Kumar Jain
  • 19,561
  • 8
  • 59
  • 90
0

"and restrict the duplicate values using distinct." at least in Postgres 9+ DISTINCT eliminates existing duplicates but not preventing or restricting its appearing.

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
0
SELECT DISTINCT A.* 
FROM aTable AS A 
     INNER JOIN 
     bTable AS B USING(columnId)
i.signori
  • 566
  • 2
  • 16