SQL Update Based on Multi-Table Computed ValuesAdd a column with a default value to an existing table in SQL ServerSQL update from one Table to another based on a ID matchHow can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableInsert into a MySQL table or update if existsFind all tables containing column with specified name - MS SQL ServerSQL select only rows with max value on a columnConvert multiple SQL code with multiple subqueries into a single queryHow to insert into one column from two table's two different column?

Count the number of paths to n

Cooking Scrambled Eggs

What should come first—characters or plot?

50-move rule: only the last 50 or any consecutive 50?

What are the occurences of total war in the Native Americans?

Unlock your Lock

Why is proof-of-work required in Bitcoin?

Did anybody find out it was Anakin who blew up the command center?

How would a low-tech device be able to alert its user?

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

Redacting URLs as an email-phishing preventative?

What is Spectral Subtraction for noise reduction?

using resizegather inside tabular: how to remove extra vertical space above and below the equation?

How to gently end involvement with an online community?

Joining lists with same elements

Duplicate instruments in unison in an orchestra

Can RMSE and MAE have the same value?

Why should a self-financing strategy be previsible?

Why is "-ber" the suffix of the last four months of the year?

How should i charge 3 lithium ion batteries?

What to look for in a spotting scope?

How to check whether a sublist exist in a huge database lists in a fast way?

How much does Commander Data weigh?

Was the Boeing 2707 design flawed?



SQL Update Based on Multi-Table Computed Values


Add a column with a default value to an existing table in SQL ServerSQL update from one Table to another based on a ID matchHow can I do an UPDATE statement with JOIN in SQL?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableInsert into a MySQL table or update if existsFind all tables containing column with specified name - MS SQL ServerSQL select only rows with max value on a columnConvert multiple SQL code with multiple subqueries into a single queryHow to insert into one column from two table's two different column?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a SQL statement that will be run daily against my db; logically, it will set a value on one table to null after a period of time has elapsed. However, determining whether that period has passed requires examining two other tables and then calculating the length of time that has passed based on the values contained therein.



My current SQL statement works for these purposes (see below) but as it relies on the use of an IN clause, there's the possibility it'll run afoul of the 1000 element limit that applies to IN clauses. I'd like to reformat this query away from relying on an IN so that it doesn't matter how many records this update is touching.



UPDATE table1 SET table1.field1 = null
WHERE table1.table1_id IN
(SELECT table1_id
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff)


How can this be rewritten such that field1 of table1 is set appropriately to null without relying on the potentially fallible IN clause? This is an Oracle db, to whatever extent that effects the possible solutions.










share|improve this question
























  • What do you mean "run afoul of the 1000 element limit"? Does your inner query return expected results?

    – Pavel Smirnov
    Mar 27 at 19:55











  • As part of a different query, I recently found out oracle caps the number of elements in an IN clause to 1000, and throws an exception when that's exceeded. It seems unlikely, but if there were a large number of records that ticked over at the same time, the IN clause could possibly trip that limit? I'm not entirely sure if it applies to a sub-query vs a straight list of values, but it's something I'm trying to stay aware of now.

    – caliber
    Mar 27 at 20:56











  • Oracle does not have such limits. It seems there was a problem with the query that threw an exception.

    – Pavel Smirnov
    Mar 27 at 21:00











  • @PavelSmirnov ORA-01795!!!

    – hbourchi
    Mar 27 at 21:45











  • @hbourchi, that is not subquery limits. It's the max size of simple values, like in ('value1', 'value2', 'value3'...'value1000')

    – Pavel Smirnov
    Mar 27 at 21:48

















1















I have a SQL statement that will be run daily against my db; logically, it will set a value on one table to null after a period of time has elapsed. However, determining whether that period has passed requires examining two other tables and then calculating the length of time that has passed based on the values contained therein.



My current SQL statement works for these purposes (see below) but as it relies on the use of an IN clause, there's the possibility it'll run afoul of the 1000 element limit that applies to IN clauses. I'd like to reformat this query away from relying on an IN so that it doesn't matter how many records this update is touching.



UPDATE table1 SET table1.field1 = null
WHERE table1.table1_id IN
(SELECT table1_id
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff)


How can this be rewritten such that field1 of table1 is set appropriately to null without relying on the potentially fallible IN clause? This is an Oracle db, to whatever extent that effects the possible solutions.










share|improve this question
























  • What do you mean "run afoul of the 1000 element limit"? Does your inner query return expected results?

    – Pavel Smirnov
    Mar 27 at 19:55











  • As part of a different query, I recently found out oracle caps the number of elements in an IN clause to 1000, and throws an exception when that's exceeded. It seems unlikely, but if there were a large number of records that ticked over at the same time, the IN clause could possibly trip that limit? I'm not entirely sure if it applies to a sub-query vs a straight list of values, but it's something I'm trying to stay aware of now.

    – caliber
    Mar 27 at 20:56











  • Oracle does not have such limits. It seems there was a problem with the query that threw an exception.

    – Pavel Smirnov
    Mar 27 at 21:00











  • @PavelSmirnov ORA-01795!!!

    – hbourchi
    Mar 27 at 21:45











  • @hbourchi, that is not subquery limits. It's the max size of simple values, like in ('value1', 'value2', 'value3'...'value1000')

    – Pavel Smirnov
    Mar 27 at 21:48













1












1








1








I have a SQL statement that will be run daily against my db; logically, it will set a value on one table to null after a period of time has elapsed. However, determining whether that period has passed requires examining two other tables and then calculating the length of time that has passed based on the values contained therein.



My current SQL statement works for these purposes (see below) but as it relies on the use of an IN clause, there's the possibility it'll run afoul of the 1000 element limit that applies to IN clauses. I'd like to reformat this query away from relying on an IN so that it doesn't matter how many records this update is touching.



UPDATE table1 SET table1.field1 = null
WHERE table1.table1_id IN
(SELECT table1_id
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff)


How can this be rewritten such that field1 of table1 is set appropriately to null without relying on the potentially fallible IN clause? This is an Oracle db, to whatever extent that effects the possible solutions.










share|improve this question














I have a SQL statement that will be run daily against my db; logically, it will set a value on one table to null after a period of time has elapsed. However, determining whether that period has passed requires examining two other tables and then calculating the length of time that has passed based on the values contained therein.



My current SQL statement works for these purposes (see below) but as it relies on the use of an IN clause, there's the possibility it'll run afoul of the 1000 element limit that applies to IN clauses. I'd like to reformat this query away from relying on an IN so that it doesn't matter how many records this update is touching.



UPDATE table1 SET table1.field1 = null
WHERE table1.table1_id IN
(SELECT table1_id
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff)


How can this be rewritten such that field1 of table1 is set appropriately to null without relying on the potentially fallible IN clause? This is an Oracle db, to whatever extent that effects the possible solutions.







sql oracle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 19:40









calibercaliber

82 bronze badges




82 bronze badges















  • What do you mean "run afoul of the 1000 element limit"? Does your inner query return expected results?

    – Pavel Smirnov
    Mar 27 at 19:55











  • As part of a different query, I recently found out oracle caps the number of elements in an IN clause to 1000, and throws an exception when that's exceeded. It seems unlikely, but if there were a large number of records that ticked over at the same time, the IN clause could possibly trip that limit? I'm not entirely sure if it applies to a sub-query vs a straight list of values, but it's something I'm trying to stay aware of now.

    – caliber
    Mar 27 at 20:56











  • Oracle does not have such limits. It seems there was a problem with the query that threw an exception.

    – Pavel Smirnov
    Mar 27 at 21:00











  • @PavelSmirnov ORA-01795!!!

    – hbourchi
    Mar 27 at 21:45











  • @hbourchi, that is not subquery limits. It's the max size of simple values, like in ('value1', 'value2', 'value3'...'value1000')

    – Pavel Smirnov
    Mar 27 at 21:48

















  • What do you mean "run afoul of the 1000 element limit"? Does your inner query return expected results?

    – Pavel Smirnov
    Mar 27 at 19:55











  • As part of a different query, I recently found out oracle caps the number of elements in an IN clause to 1000, and throws an exception when that's exceeded. It seems unlikely, but if there were a large number of records that ticked over at the same time, the IN clause could possibly trip that limit? I'm not entirely sure if it applies to a sub-query vs a straight list of values, but it's something I'm trying to stay aware of now.

    – caliber
    Mar 27 at 20:56











  • Oracle does not have such limits. It seems there was a problem with the query that threw an exception.

    – Pavel Smirnov
    Mar 27 at 21:00











  • @PavelSmirnov ORA-01795!!!

    – hbourchi
    Mar 27 at 21:45











  • @hbourchi, that is not subquery limits. It's the max size of simple values, like in ('value1', 'value2', 'value3'...'value1000')

    – Pavel Smirnov
    Mar 27 at 21:48
















What do you mean "run afoul of the 1000 element limit"? Does your inner query return expected results?

– Pavel Smirnov
Mar 27 at 19:55





What do you mean "run afoul of the 1000 element limit"? Does your inner query return expected results?

– Pavel Smirnov
Mar 27 at 19:55













As part of a different query, I recently found out oracle caps the number of elements in an IN clause to 1000, and throws an exception when that's exceeded. It seems unlikely, but if there were a large number of records that ticked over at the same time, the IN clause could possibly trip that limit? I'm not entirely sure if it applies to a sub-query vs a straight list of values, but it's something I'm trying to stay aware of now.

– caliber
Mar 27 at 20:56





As part of a different query, I recently found out oracle caps the number of elements in an IN clause to 1000, and throws an exception when that's exceeded. It seems unlikely, but if there were a large number of records that ticked over at the same time, the IN clause could possibly trip that limit? I'm not entirely sure if it applies to a sub-query vs a straight list of values, but it's something I'm trying to stay aware of now.

– caliber
Mar 27 at 20:56













Oracle does not have such limits. It seems there was a problem with the query that threw an exception.

– Pavel Smirnov
Mar 27 at 21:00





Oracle does not have such limits. It seems there was a problem with the query that threw an exception.

– Pavel Smirnov
Mar 27 at 21:00













@PavelSmirnov ORA-01795!!!

– hbourchi
Mar 27 at 21:45





@PavelSmirnov ORA-01795!!!

– hbourchi
Mar 27 at 21:45













@hbourchi, that is not subquery limits. It's the max size of simple values, like in ('value1', 'value2', 'value3'...'value1000')

– Pavel Smirnov
Mar 27 at 21:48





@hbourchi, that is not subquery limits. It's the max size of simple values, like in ('value1', 'value2', 'value3'...'value1000')

– Pavel Smirnov
Mar 27 at 21:48












1 Answer
1






active

oldest

votes


















0















Not sure if I understand your question fully but another way to write your query is using EXISTS. something like this:



UPDATE table1
SET table1.field1 = null
WHERE exists
(SELECT null
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff
and table1.table1_id = tmp_table.table1_id)





share|improve this answer

























  • The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

    – caliber
    Mar 27 at 20:54










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385260%2fsql-update-based-on-multi-table-computed-values%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









0















Not sure if I understand your question fully but another way to write your query is using EXISTS. something like this:



UPDATE table1
SET table1.field1 = null
WHERE exists
(SELECT null
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff
and table1.table1_id = tmp_table.table1_id)





share|improve this answer

























  • The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

    – caliber
    Mar 27 at 20:54















0















Not sure if I understand your question fully but another way to write your query is using EXISTS. something like this:



UPDATE table1
SET table1.field1 = null
WHERE exists
(SELECT null
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff
and table1.table1_id = tmp_table.table1_id)





share|improve this answer

























  • The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

    – caliber
    Mar 27 at 20:54













0














0










0









Not sure if I understand your question fully but another way to write your query is using EXISTS. something like this:



UPDATE table1
SET table1.field1 = null
WHERE exists
(SELECT null
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff
and table1.table1_id = tmp_table.table1_id)





share|improve this answer













Not sure if I understand your question fully but another way to write your query is using EXISTS. something like this:



UPDATE table1
SET table1.field1 = null
WHERE exists
(SELECT null
FROM (SELECT table1_id, (SYSDATE - MAX(table2.date_field)) days
WHERE table1.field1 IS NOT null
AND table1.table1_id = table2.table1_fk
AND table2.table3_fk = table3.table3_id
AND (table3.type = X OR table3.type = Y)
GROUP BY table1_id) tmp_table
WHERE days >= cutoff
and table1.table1_id = tmp_table.table1_id)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 20:06









hbourchihbourchi

1847 bronze badges




1847 bronze badges















  • The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

    – caliber
    Mar 27 at 20:54

















  • The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

    – caliber
    Mar 27 at 20:54
















The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

– caliber
Mar 27 at 20:54





The sub-sub query is missing a FROM statement, and I changed the SELECT null to SELECT table1.table1_id, but this seems to work! I figured it was going to be something simple, thanks for this.

– caliber
Mar 27 at 20:54








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55385260%2fsql-update-based-on-multi-table-computed-values%23new-answer', 'question_page');

);

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







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해