Generate a sequence number for every 3 rows in SQL
up vote
3
down vote
favorite
I need to generate a sequence number
for every three rows
with some range. can this be done without iterations.
Example:
sequence
--------
1
1
1
2
2
2
3
3
3
sql sql-server tsql
add a comment |
up vote
3
down vote
favorite
I need to generate a sequence number
for every three rows
with some range. can this be done without iterations.
Example:
sequence
--------
1
1
1
2
2
2
3
3
3
sql sql-server tsql
There is still problem.you have not mention in which scenario you want such sequence. What is the actual query like ?Knowing that can make query easier .
– KumarHarsh
Jan 2 '15 at 6:29
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I need to generate a sequence number
for every three rows
with some range. can this be done without iterations.
Example:
sequence
--------
1
1
1
2
2
2
3
3
3
sql sql-server tsql
I need to generate a sequence number
for every three rows
with some range. can this be done without iterations.
Example:
sequence
--------
1
1
1
2
2
2
3
3
3
sql sql-server tsql
sql sql-server tsql
edited Nov 22 at 5:37
Cœur
17.1k9102140
17.1k9102140
asked Jan 1 '15 at 4:17
saran
255
255
There is still problem.you have not mention in which scenario you want such sequence. What is the actual query like ?Knowing that can make query easier .
– KumarHarsh
Jan 2 '15 at 6:29
add a comment |
There is still problem.you have not mention in which scenario you want such sequence. What is the actual query like ?Knowing that can make query easier .
– KumarHarsh
Jan 2 '15 at 6:29
There is still problem.you have not mention in which scenario you want such sequence. What is the actual query like ?Knowing that can make query easier .
– KumarHarsh
Jan 2 '15 at 6:29
There is still problem.you have not mention in which scenario you want such sequence. What is the actual query like ?Knowing that can make query easier .
– KumarHarsh
Jan 2 '15 at 6:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
17
down vote
accepted
Use this Analytic function
SELECT ( ( Row_number()OVER(ORDER BY order_by_column ) - 1 ) / 3 ) + 1 seq_no,
*
FROM tablename
3
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
accepted
Use this Analytic function
SELECT ( ( Row_number()OVER(ORDER BY order_by_column ) - 1 ) / 3 ) + 1 seq_no,
*
FROM tablename
3
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
add a comment |
up vote
17
down vote
accepted
Use this Analytic function
SELECT ( ( Row_number()OVER(ORDER BY order_by_column ) - 1 ) / 3 ) + 1 seq_no,
*
FROM tablename
3
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
add a comment |
up vote
17
down vote
accepted
up vote
17
down vote
accepted
Use this Analytic function
SELECT ( ( Row_number()OVER(ORDER BY order_by_column ) - 1 ) / 3 ) + 1 seq_no,
*
FROM tablename
Use this Analytic function
SELECT ( ( Row_number()OVER(ORDER BY order_by_column ) - 1 ) / 3 ) + 1 seq_no,
*
FROM tablename
answered Jan 1 '15 at 4:19
Pரதீப்
74.9k1075109
74.9k1075109
3
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
add a comment |
3
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
3
3
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
Pretty clever use of integer division.
– Jason Faulkner
Jan 1 '15 at 4:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27728225%2fgenerate-a-sequence-number-for-every-3-rows-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There is still problem.you have not mention in which scenario you want such sequence. What is the actual query like ?Knowing that can make query easier .
– KumarHarsh
Jan 2 '15 at 6:29