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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 order

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2014-03-27 : 06:18:31
Hi,
The below table shows a field which has Names in it.
Some names are repeated.

Name
===========
Fondation
Boris
Boris
Melly
ocitco
viz
viz
viz
viz

Question:
How can each name have a similar ID so that I get the below in order of names:

OrderID Name
==========================
1 Boris
1 Boris
2 ocitco
3 Fondation
4 Melly
5 viz
5 viz
5 viz
5 viz

Not sure if this can be done by row_number() or something different?
Thank you

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-03-27 : 06:31:43
select *,Dense_rank() OVER(ORDER BY name) from tablename

Veera
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2014-03-27 : 06:33:41
Hi, can the same be done using row_number()?
thanks
Go to Top of Page

sqlsaga
Yak Posting Veteran

93 Posts

Posted - 2014-03-27 : 11:44:09
Read this article about ranking functions to get a good understanding of what each one does...

http://sqlsaga.com/sql-server/what-is-the-difference-between-rank-dense_rank-row_number-and-ntile-in-sql-server/


Visit www.sqlsaga.com for more t-sql snippets and BI related how to's.
Go to Top of Page
   

- Advertisement -