Updating row numbers in chunks
I need to do batching of rows, so that batch jobs will pick up appropriate records for processing. Currently I write several update queries for it like so:
update salestable
set BATCHNO=1
where status=1 and id in (
select top 1000 id from salestable where status=1)
update salestable
set BATCHNO=2
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1))
update salestable
set BATCHNO=3
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1,2))
...
...
... on and on
Is there a single query available for this?
sql
add a comment |
I need to do batching of rows, so that batch jobs will pick up appropriate records for processing. Currently I write several update queries for it like so:
update salestable
set BATCHNO=1
where status=1 and id in (
select top 1000 id from salestable where status=1)
update salestable
set BATCHNO=2
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1))
update salestable
set BATCHNO=3
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1,2))
...
...
... on and on
Is there a single query available for this?
sql
Your subqueries make no sense, because you are usingTOPwithoutORDER BY.
– Tim Biegeleisen
Nov 14 '18 at 10:57
1
I'd say userow_numberto generate a consecutive sequence ordered by ID. You can calculate the batch number by doingseq mod 1000(or actually((seq-1) mod 1000) + 1). Use that select as input for the update statement.
– GolezTrol
Nov 14 '18 at 10:57
@GolezTrol For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on.
– MYGz
Nov 14 '18 at 12:08
Sorry, yes, division of course. But you get the general idea :o) Sorry I didn't have time to figure out a syntactically correct, SQL server compatible version of this, but I see Tim has helped you out there.
– GolezTrol
Nov 14 '18 at 13:48
@GolezTrol Yep. Thank you.
– MYGz
Nov 15 '18 at 1:54
add a comment |
I need to do batching of rows, so that batch jobs will pick up appropriate records for processing. Currently I write several update queries for it like so:
update salestable
set BATCHNO=1
where status=1 and id in (
select top 1000 id from salestable where status=1)
update salestable
set BATCHNO=2
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1))
update salestable
set BATCHNO=3
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1,2))
...
...
... on and on
Is there a single query available for this?
sql
I need to do batching of rows, so that batch jobs will pick up appropriate records for processing. Currently I write several update queries for it like so:
update salestable
set BATCHNO=1
where status=1 and id in (
select top 1000 id from salestable where status=1)
update salestable
set BATCHNO=2
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1))
update salestable
set BATCHNO=3
where status=1 and id in (
select top 1000 id from salestable where status=1
and batchno not in (1,2))
...
...
... on and on
Is there a single query available for this?
sql
sql
edited Nov 14 '18 at 11:13
Rahul Neekhra
6001627
6001627
asked Nov 14 '18 at 10:51
MYGzMYGz
8,45852250
8,45852250
Your subqueries make no sense, because you are usingTOPwithoutORDER BY.
– Tim Biegeleisen
Nov 14 '18 at 10:57
1
I'd say userow_numberto generate a consecutive sequence ordered by ID. You can calculate the batch number by doingseq mod 1000(or actually((seq-1) mod 1000) + 1). Use that select as input for the update statement.
– GolezTrol
Nov 14 '18 at 10:57
@GolezTrol For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on.
– MYGz
Nov 14 '18 at 12:08
Sorry, yes, division of course. But you get the general idea :o) Sorry I didn't have time to figure out a syntactically correct, SQL server compatible version of this, but I see Tim has helped you out there.
– GolezTrol
Nov 14 '18 at 13:48
@GolezTrol Yep. Thank you.
– MYGz
Nov 15 '18 at 1:54
add a comment |
Your subqueries make no sense, because you are usingTOPwithoutORDER BY.
– Tim Biegeleisen
Nov 14 '18 at 10:57
1
I'd say userow_numberto generate a consecutive sequence ordered by ID. You can calculate the batch number by doingseq mod 1000(or actually((seq-1) mod 1000) + 1). Use that select as input for the update statement.
– GolezTrol
Nov 14 '18 at 10:57
@GolezTrol For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on.
– MYGz
Nov 14 '18 at 12:08
Sorry, yes, division of course. But you get the general idea :o) Sorry I didn't have time to figure out a syntactically correct, SQL server compatible version of this, but I see Tim has helped you out there.
– GolezTrol
Nov 14 '18 at 13:48
@GolezTrol Yep. Thank you.
– MYGz
Nov 15 '18 at 1:54
Your subqueries make no sense, because you are using
TOP without ORDER BY.– Tim Biegeleisen
Nov 14 '18 at 10:57
Your subqueries make no sense, because you are using
TOP without ORDER BY.– Tim Biegeleisen
Nov 14 '18 at 10:57
1
1
I'd say use
row_number to generate a consecutive sequence ordered by ID. You can calculate the batch number by doing seq mod 1000 (or actually ((seq-1) mod 1000) + 1). Use that select as input for the update statement.– GolezTrol
Nov 14 '18 at 10:57
I'd say use
row_number to generate a consecutive sequence ordered by ID. You can calculate the batch number by doing seq mod 1000 (or actually ((seq-1) mod 1000) + 1). Use that select as input for the update statement.– GolezTrol
Nov 14 '18 at 10:57
@GolezTrol For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on.
– MYGz
Nov 14 '18 at 12:08
@GolezTrol For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on.
– MYGz
Nov 14 '18 at 12:08
Sorry, yes, division of course. But you get the general idea :o) Sorry I didn't have time to figure out a syntactically correct, SQL server compatible version of this, but I see Tim has helped you out there.
– GolezTrol
Nov 14 '18 at 13:48
Sorry, yes, division of course. But you get the general idea :o) Sorry I didn't have time to figure out a syntactically correct, SQL server compatible version of this, but I see Tim has helped you out there.
– GolezTrol
Nov 14 '18 at 13:48
@GolezTrol Yep. Thank you.
– MYGz
Nov 15 '18 at 1:54
@GolezTrol Yep. Thank you.
– MYGz
Nov 15 '18 at 1:54
add a comment |
1 Answer
1
active
oldest
votes
Something like this might work:
WITH cte AS (
SELECT id, BATCHNO, ROW_NUMBER() OVER (ORDER BY id) rn
FROM yourTable
WHERE status = 1
)
UPDATE cte
SET BATCHNO = ((rn - 1) / 1000) + 1;
Note carefully that I have assumed an ordering for the batches using the id column. In your current subqueries, you are using TOP without ORDER BY, which is a fairly undefined thing. We can only speak of the top 1000 records with regard to some ordering.
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53298458%2fupdating-row-numbers-in-chunks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like this might work:
WITH cte AS (
SELECT id, BATCHNO, ROW_NUMBER() OVER (ORDER BY id) rn
FROM yourTable
WHERE status = 1
)
UPDATE cte
SET BATCHNO = ((rn - 1) / 1000) + 1;
Note carefully that I have assumed an ordering for the batches using the id column. In your current subqueries, you are using TOP without ORDER BY, which is a fairly undefined thing. We can only speak of the top 1000 records with regard to some ordering.
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
add a comment |
Something like this might work:
WITH cte AS (
SELECT id, BATCHNO, ROW_NUMBER() OVER (ORDER BY id) rn
FROM yourTable
WHERE status = 1
)
UPDATE cte
SET BATCHNO = ((rn - 1) / 1000) + 1;
Note carefully that I have assumed an ordering for the batches using the id column. In your current subqueries, you are using TOP without ORDER BY, which is a fairly undefined thing. We can only speak of the top 1000 records with regard to some ordering.
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
add a comment |
Something like this might work:
WITH cte AS (
SELECT id, BATCHNO, ROW_NUMBER() OVER (ORDER BY id) rn
FROM yourTable
WHERE status = 1
)
UPDATE cte
SET BATCHNO = ((rn - 1) / 1000) + 1;
Note carefully that I have assumed an ordering for the batches using the id column. In your current subqueries, you are using TOP without ORDER BY, which is a fairly undefined thing. We can only speak of the top 1000 records with regard to some ordering.
Something like this might work:
WITH cte AS (
SELECT id, BATCHNO, ROW_NUMBER() OVER (ORDER BY id) rn
FROM yourTable
WHERE status = 1
)
UPDATE cte
SET BATCHNO = ((rn - 1) / 1000) + 1;
Note carefully that I have assumed an ordering for the batches using the id column. In your current subqueries, you are using TOP without ORDER BY, which is a fairly undefined thing. We can only speak of the top 1000 records with regard to some ordering.
edited Nov 14 '18 at 12:15
Gordon Linoff
770k35304404
770k35304404
answered Nov 14 '18 at 11:01
Tim BiegeleisenTim Biegeleisen
224k1391143
224k1391143
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
add a comment |
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on. Update your answer, I'll accept.
– MYGz
Nov 14 '18 at 12:05
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.
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%2f53298458%2fupdating-row-numbers-in-chunks%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
Your subqueries make no sense, because you are using
TOPwithoutORDER BY.– Tim Biegeleisen
Nov 14 '18 at 10:57
1
I'd say use
row_numberto generate a consecutive sequence ordered by ID. You can calculate the batch number by doingseq mod 1000(or actually((seq-1) mod 1000) + 1). Use that select as input for the update statement.– GolezTrol
Nov 14 '18 at 10:57
@GolezTrol For me division does the job instead of modulus since I want to give numbering to rows in batch of 1000's. For eg 1st 1000's as 1, next 1000 as 2, and so on.
– MYGz
Nov 14 '18 at 12:08
Sorry, yes, division of course. But you get the general idea :o) Sorry I didn't have time to figure out a syntactically correct, SQL server compatible version of this, but I see Tim has helped you out there.
– GolezTrol
Nov 14 '18 at 13:48
@GolezTrol Yep. Thank you.
– MYGz
Nov 15 '18 at 1:54