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 2012 Forums
 Transact-SQL (2012)
 Group Number

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-02-10 : 09:51:16
hi

I have this data:

A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
C 1
C 2
C 3
C 4
C 5

I would like to have a group id number based on every 3 rows. The result should look like this

1 A 1
1 A 2
1 A 3
2 A 4
2 A 5
1 B 1
1 B 2
1 B 3
2 B 4
2 B 5
1 C 1
1 C 2
1 C 3
2 C 4
2 C 5

How should I go about it? Thanks a lot

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-11 : 00:59:56
[code]
SELECT ((ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col2)-1)/3)+1 AS Seq,Col1,Col2
FROM Table
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-02-11 : 08:29:56
thanks visakh16
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-12 : 00:02:29
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -