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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select distinct and other fields: aggregate function for text?

Status
Not open for further replies.

capooti

Programmer
Mar 19, 2002
14
0
0
IT
Hi, this may be very simple, but I don't know how to do:
suppose I have the following TableX

COD NAME
1 Tim
2 Jason
2 Paul
3 Mark

I want a distinct on COD field. If I try this

SELECT DISTINCT(COD), NAME FROM TableX

then I will receive the whole table.
How can I receive something like this:

COD NAME
1 Tim
2 Paul or Jason, is not impourtant form me
3 Mark

Are there in T-SQL aggregate function for text? I.e. the first value found (this case Jason).

Thanks in advance

 
A solution could be the following:

SELECT TableX.COD, TableX.NAME FROM TableX WHERE TableX.NAME = (SELECT TOP 1 TableX.NAME FROM TableX AS TableX_1 WHERE TableX_1.NAME = TableX.NAME)
 
Please check the above query,
I think you have to add the TableX_1.COD = TableX.COD instead of TableX_1.NAME = TableX.NAME in above query.

Madhu.
 
Thank you all, but I found the following even easier (and ANSI, not like TOP statment)

SELECT DISTINCT(COD), max(NAME) FROM TableX
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top