Let’ say I have 2 sql queries. Table A contains,
ID
--
1
1
1
2
3
4
This query,
Select distinct ID1 FROM A
gives me,
ID
--
1
2
3
4
Second one
Select ID2 FROM B
which gives me,
ID2
--
8
21
33
43
How 2 get this record set?
ID1 ID2
--- ---
1 8
2 21
3 33
4 43
You did not specify what version of sql server but if you are using sql server 2008+, one way that you can do this is by adding the
row_number()to each table and then joining on therow_number():See SQL Fiddle with Demo
If you want to only use
DISTINCTvalues, then you should be able to use:See SQL Fiddle with Demo
If you have a different number of rows in each table, then you might want to use a
FULL OUTER JOIN:See SQL Fiddle with Demo