Select distinct from multiple tables

I have multiple tables and from each table I need one column (column have same name).  But I cannot use 'UNION' because 'UNION' will put all data into one output column (worse, there is no common key between tables), Example:





What I'm try to do is to have output (by select distinct) from multiple tables and ouput the columns side by side, example:

tab1.column_1      tab2.column_1
----------------       ----------------
1a                       6a
2b                       7b
3c                       8c
4d                       9d
5e                       10e

This should be easy but I cannot come up with the correct 'SELECT DISTINCT' for the ouput.

Thannk you,
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe WoodhousePrincipal ConsultantCommented:
1> select   col, id=identity(18)
2> into     #distinct1
3> from     #test1
4> group by col
5> select   col, id=identity(18)
6> into     #distinct2
7> from     #test2
8> group by col
9> go
(5 rows affected)
(5 rows affected)

1> select A.col, B.col
2> from #distinct1 A, #distinct2 B
3> where =
5> go
 col                            col
 ------------------------------ ------------------------------
 1a                             10e
 2b                             6a
 3c                             7b
 4d                             8c
 5e                             9d

(5 rows affected)

Note that these are in fact correct results - the string "10e" sorts before "6a".

This will table scan your base tables as well as the temp tables. You don't mention how much data you have so this could be a problem!

Good luck!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tranicusAuthor Commented:
Great Idea...  Identity columns.

Thank you,
Joe WoodhousePrincipal ConsultantCommented:
Using an identity like that doesn't work in every version, so if you're using an old version we'd have to try something else. Good luck!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.