-
SQL question (it should be simple)
I suppose it's a simple question, but I'm a complete newbie with SQL.
I have a 2-column table with the entries like these:
John - Smith
John - Lennon
Paul - McCartney
Paul - Mauriat
Paul - Smith
Stephen - King
Stephen - Hawking
etc...
I need a table where 1st column contain only unique elements and whatever value in 2nd column, like
John - Lennon
Paul - McCartney
Stephen - King
-
 Originally Posted by regul8or
I suppose it's a simple question, but I'm a complete newbie with SQL.
I have a 2-column table with the entries like these:
John - Smith
John - Lennon
Paul - McCartney
Paul - Mauriat
Paul - Smith
Stephen - King
Stephen - Hawking
etc...
I need a table where 1st column contain only unique elements and whatever value in 2nd column, like
John - Lennon
Paul - McCartney
Stephen - King
This is one way you could do it:
SELECT tt1.firstname,
(select top 1 tt2.lastname from testtable tt2 where tt1.firstname = tt2.firstname) as LastName
from TestTable tt1
group by firstname
Is there any criteria determining which John, Paul, Stephen, etc. would be selected since there are multiples of each?
-
Thanks, Tony
That's solved it.
-
You can also try with
select distnct firstname,lastname from tablename;
-
This will return a whole table (all Johns, Pauls etc) and it's clearly not what I needed.
Similar Threads
-
By rperez in forum Database
Replies: 5
Last Post: 01-02-2009, 04:14 PM
-
By David Satz in forum Database
Replies: 1
Last Post: 05-09-2002, 04:09 PM
-
By Jeremy in forum Database
Replies: 0
Last Post: 01-24-2002, 04:13 PM
-
Replies: 3
Last Post: 05-19-2000, 02:51 AM
-
By Rodolphe Pomerleau in forum Database
Replies: 1
Last Post: 04-13-2000, 04:55 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks