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 2005 Forums
 Transact-SQL (2005)
 is it possible with query (Giving Groups numbers)

Author  Topic 

dirac
Starting Member

12 Posts

Posted - 2008-01-17 : 13:19:21
Hi, is it possible in sql to query a table by giving the groups numbers for example i have a two columned table and the data is like this:

c1 c2
------------
a red
a blue
a brown
b white
b gray
c maroon
and all iwant to get with a query is this:

Nu. c1 c2
---------------
1 a red
1 a blue
1 a brown
2 b white
2 b gray
3 c maroon
as you see the original order of the table and the datas of the columns are the same but i only want to give a group number like above:)

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-01-17 : 13:25:47
Use ROW_NUMBER() function.

Check this for examples of how to use ROW_NUMBER():
http://technet.microsoft.com/en-us/library/ms186734.aspx

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dirac
Starting Member

12 Posts

Posted - 2008-01-17 : 13:49:40
hi first of all thanks for your answer ,
i think you got me wrong and i think you mean the row_number over partition on that page ilooked at that is amazing function but it gives an order in each group not numering the groups,
Go to Top of Page

dirac
Starting Member

12 Posts

Posted - 2008-01-17 : 14:04:12
thank you anyway i have just found the solution from another site if anybody wonders i can share it can be possible with a dense_rank() function, thanks anyway.
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2008-01-17 : 15:16:24
You got the results but below query also helps to get your desired results..
SELECT DENSE_RANK () OVER (ORDER BY C1) AS 'C1', * from table
Go to Top of Page
   

- Advertisement -