Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT distinct two columns to one long row? 1

Status
Not open for further replies.

Bohiti

MIS
Mar 25, 2002
10
0
0
US
Example of table named "Servers":
Code:
ServerName     Admin1     Admin2
----------     ------     ------
SERVER1        Mike       Joe
SERVER2        John       Kevin
SERVER3        Mike       John
SERVER4        Joe        Stacy

I'd like to query for all (distinct) Admins. The results would look something like:

Code:
Admin
-----
Joe
John
Kevin
Mike
Stacy

This has been bugging me for some time. I realize I could use a second table to store names, but this is a really simple system, and I'd like to avoid that if possible. Can you come up with a SQL query to pull this off? Thanks very much!
 
SELECT DISTINCT ADMIN1 AS "Admin" FROM SERVERS
UNION
SELECT DISTINCT ADMIN2 FROM SERVERS
ORDER BY ADMIN
 
lespaul,

wouldn't that introduce double entries if admin exsists both in admin1 and admin2?



--------------------
Procrastinate Now!
 
Thank you lespaul!
Crowley, it doesn't show duplicate fields. However, I had to remove the "order by" clause, it was saying that "Admin" isn't a selected field. Without the "order by" clause, it is already sorted, however. Shrug.

Thanks again, I wasn't familiar with the Union statement.
 
hmm, I thought the union statement worked differently...

--------------------
Procrastinate Now!
 
SELECT Admin1 AS Admin FROM Servers
UNION SELECT Admin2 FROM Servers
ORDER BY 1;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top