5

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
  • 69
  • 1
  • 1
  • 4

5 Answers5

12

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,499
  • 7
  • 39
  • 56
1

If distinct is not restricted

SELECT DISTINCT a.*, b.pk
FROM    A1 a
INNER JOIN A2 b ON (a.[test] = b.fk)
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
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
  • 20,239
  • 9
  • 63
  • 92
0

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

0
SELECT DISTINCT A.* 
FROM aTable AS A 
     INNER JOIN 
     bTable AS B USING(columnId)
i.signori
  • 585
  • 3
  • 16
Roman Ivasyshyn
  • 118
  • 1
  • 8