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
 Need assistance with sequence

Author  Topic 

Silumadbor
Starting Member

2 Posts

Posted - 2015-04-23 : 17:24:27
In select query, how can i limit sequence upto 5 and get result like below?

Rec_ID Company Sequence#
123 ABC1 1
124 ABC2 2
125 ABC3 3
126 ABC4 4
127 ABC5 5
128 ABC6 1
129 ABC7 2
130 ABC8 3
131 ABC9 4
132 ABC10 5
133 ABC11 1
134 ABC12 2
135 ABC13 3
136 ABC14 4
137 ABC15 5
138 ABC16 1
139 ABC17 2
140 ABC18 3
141 ABC19 4
142 ABC20 5

thanks,
silu

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-04-23 : 17:32:03
You can use the ROW_NUMBER() function to achieve this with a CTE or a derived table.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2015-05-07 : 06:24:07
One way is

select Rec_ID,Company, (row_number() over (order by object_id)+4)%5+1 as Sequence# from table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Silumadbor
Starting Member

2 Posts

Posted - 2015-05-07 : 22:10:25
YOU GUYS ROCK!!! THANKS!!!
Go to Top of Page
   

- Advertisement -