Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Unique Name

Author  Topic 

eem_2055
Yak Posting Veteran

69 Posts

Posted - 2008-03-29 : 02:58:51
Hi guys,

I need your help!

May I know what can I do to get a single name using the sample table below?
toptable
name account
a 1
a 2
a 3
b 1
b 2
c 2
d 3
e 1
e 2

result will be

toptable
name account
a 1
b 2
c 2
d 3
e 2

I just want to get all single names

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-03-29 : 03:06:21
try this,

Select Name, Min(Account) AS 'Account'
From tbl
Group By Name
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-29 : 03:06:31
[code]SELECT name,MAX(account)
FROM YourTable
GROUP BY name [/code]

OR

[code]SELECT name,MIN(account)
FROM YourTable
GROUP BY name[/code]

OR

sql 2005 compatible soln.

[code]SELECT t.name,t.account
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY name ORDER BY NEWID() DESC) AS RowNo,
name,
account
FROM Table
)t
WHERE t.RowNo=1[/code]
Go to Top of Page

eem_2055
Yak Posting Veteran

69 Posts

Posted - 2008-03-29 : 03:22:23
thanks a lot! :)
Go to Top of Page

eem_2055
Yak Posting Veteran

69 Posts

Posted - 2008-03-29 : 03:27:44
but it gave the same result of records
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-29 : 03:42:32
What do you mean? You still got duplicate names? If yes, can you post full query used?
Go to Top of Page

eem_2055
Yak Posting Veteran

69 Posts

Posted - 2008-03-29 : 03:44:17
same nubmer of records
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-29 : 10:47:03
Please post your full query used if you need more help.
Go to Top of Page
   

- Advertisement -