Delete records if not present in subqueryWhy did Postgres UPDATE take 39 hours?Improve performance on...
Why are `&array` and `array` pointing to the same address?
How do I avoid the "chosen hero" feeling?
How to modify 'inter arma enim silent leges' to mean 'in a time of crisis, the law falls silent'?
How bad is a Computer Science course that doesn't teach Design Patterns?
Did the characters in Moving Pictures not know about cameras like Twoflower's?
Can a planet be tidally unlocked?
Have any astronauts or cosmonauts died in space?
Manager has noticed coworker's excessive breaks. Should I warn him?
How do I handle a blinded enemy which wants to attack someone it's sure is there?
The Longest Chess Game
How to play songs that contain one guitar when we have two or more guitarists?
Empty optional argument or Not giving optional argument at all?
How can I portray body horror and still be sensitive to people with disabilities?
Does the Holy Ark weigh 4 tons?
Why is opening a file faster than reading variable content?
Aliased pipeline using head and cut
What does @ mean in a hostname in DNS configuration?
What is the difference between crontab -e and nano /etc/crontab?
Why is Bernie Sanders maximum accepted donation on actblue $5600?
Can I use the LastResort font in a web page?
Smallest possible mole
What if you do not believe in the project benefits?
Which part is the tail in 人参{にんじん}の尻尾{しっぽ}
Taking an academic pseudonym?
Delete records if not present in subquery
Why did Postgres UPDATE take 39 hours?Improve performance on concurrent UPDATEs for a timestamp column in PostgresSlow fulltext search due to wildly inaccurate row estimatesDelete duplicate records with no change in between> 30 sec slow queryWhy is my PostgreSQL expression index not being used when I ORDER BY in a subquery?Find most frequent values for a given columnPostgreSQL query is slow when return LineString dataWhy is this query with WHERE, ORDER BY and LIMIT so slow?PostgreSQL 9.5 query performance depends on JOINed column in SELECT clause
I want to delete records from a table that are not found in a subquery. But it's not having the desired result.
The table has multiple rows per SKU. Each price row has a valid_from
and valid_to
date fields. I want to erase all prices but keep those that are currently valid (where valid_to
is greater or equal to current date). I want to maintain future prices (where valid_from
is in the future), but one step at a time.
Here's what I tried:
delete from prices
where prices.id <> (
select distinct on (sku) prices.id
from prices p2
where
p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
and prices.id = p2.id
order by sku, p2.valid_from desc, p2.inserted_at desc
)
The table has around 500k rows. This subquery returns around 23k rows. I expect that my above query will delete everything but those 23k rows.
select distinct on (sku) prices.id
from prices
where
valid_to >= CURRENT_DATE
and "type" = 'regular'
order by sku, valid_from desc, inserted_at desc
But it's not deleting anything. Why does it not work the way I expect it to?
CREATE TABLE "public"."prices" (
"id" Bigint DEFAULT nextval('prices_id_seq'::regclass) NOT NULL,
"type" Character Varying( 255 ) NOT NULL,
"unit_price" Numeric( 11, 3 ) NOT NULL,
"wholesale_price" Numeric( 11, 3 ),
"min_wholesale_quantity" Numeric( 11, 3 ),
"valid_from" Date NOT NULL,
"valid_to" Date NOT NULL,
"product_cost" Numeric( 11, 3 ) NOT NULL,
"tax_rate" Numeric( 11, 3 ) NOT NULL,
"store_product_id" Bigint,
"inserted_at" Timestamp Without Time Zone NOT NULL,
"updated_at" Timestamp Without Time Zone NOT NULL,
"sku" Character Varying( 2044 ),
PRIMARY KEY ( "id" ) );
CREATE INDEX "prices_store_product_id_index" ON "public"."prices" USING btree( "store_product_id" Asc NULLS Last );
CREATE INDEX "index_sku" ON "public"."prices" USING btree( "sku" Asc NULLS Last );
ALTER TABLE "public"."prices"
ADD CONSTRAINT "prices_store_product_id_fkey" FOREIGN KEY ( "store_product_id" )
REFERENCES "public"."store_products" ( "id" ) MATCH SIMPLE
ON DELETE Cascade
ON UPDATE No Action;
postgresql delete postgresql-9.5
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
|
show 3 more comments
I want to delete records from a table that are not found in a subquery. But it's not having the desired result.
The table has multiple rows per SKU. Each price row has a valid_from
and valid_to
date fields. I want to erase all prices but keep those that are currently valid (where valid_to
is greater or equal to current date). I want to maintain future prices (where valid_from
is in the future), but one step at a time.
Here's what I tried:
delete from prices
where prices.id <> (
select distinct on (sku) prices.id
from prices p2
where
p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
and prices.id = p2.id
order by sku, p2.valid_from desc, p2.inserted_at desc
)
The table has around 500k rows. This subquery returns around 23k rows. I expect that my above query will delete everything but those 23k rows.
select distinct on (sku) prices.id
from prices
where
valid_to >= CURRENT_DATE
and "type" = 'regular'
order by sku, valid_from desc, inserted_at desc
But it's not deleting anything. Why does it not work the way I expect it to?
CREATE TABLE "public"."prices" (
"id" Bigint DEFAULT nextval('prices_id_seq'::regclass) NOT NULL,
"type" Character Varying( 255 ) NOT NULL,
"unit_price" Numeric( 11, 3 ) NOT NULL,
"wholesale_price" Numeric( 11, 3 ),
"min_wholesale_quantity" Numeric( 11, 3 ),
"valid_from" Date NOT NULL,
"valid_to" Date NOT NULL,
"product_cost" Numeric( 11, 3 ) NOT NULL,
"tax_rate" Numeric( 11, 3 ) NOT NULL,
"store_product_id" Bigint,
"inserted_at" Timestamp Without Time Zone NOT NULL,
"updated_at" Timestamp Without Time Zone NOT NULL,
"sku" Character Varying( 2044 ),
PRIMARY KEY ( "id" ) );
CREATE INDEX "prices_store_product_id_index" ON "public"."prices" USING btree( "store_product_id" Asc NULLS Last );
CREATE INDEX "index_sku" ON "public"."prices" USING btree( "sku" Asc NULLS Last );
ALTER TABLE "public"."prices"
ADD CONSTRAINT "prices_store_product_id_fkey" FOREIGN KEY ( "store_product_id" )
REFERENCES "public"."store_products" ( "id" ) MATCH SIMPLE
ON DELETE Cascade
ON UPDATE No Action;
postgresql delete postgresql-9.5
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
2
Inside the subquery you require " and prices.id = p2.id", outside you specify "<>". An id can't be equal and different at the same time.
– Gerard H. Pille
Jul 5 '18 at 22:32
Is prices.id not unique?
– Gerard H. Pille
Jul 5 '18 at 23:03
Please Edit your question and add theCREATE TABLE
statements for the tables in question adding the desired output. Those answering need a Minimal, Complete, and Verifiable example. Use formatted text please, no screen shots.
– Evan Carroll
Jul 5 '18 at 23:07
@GerardH.Pille each sku can have multiple prices, wherevalid_from
andvalid_to
is in the past or future. I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those wherevalid_from
is in the future), but one step at a time.
– Mohamad
Jul 5 '18 at 23:12
@EvanCarroll done.
– Mohamad
Jul 5 '18 at 23:12
|
show 3 more comments
I want to delete records from a table that are not found in a subquery. But it's not having the desired result.
The table has multiple rows per SKU. Each price row has a valid_from
and valid_to
date fields. I want to erase all prices but keep those that are currently valid (where valid_to
is greater or equal to current date). I want to maintain future prices (where valid_from
is in the future), but one step at a time.
Here's what I tried:
delete from prices
where prices.id <> (
select distinct on (sku) prices.id
from prices p2
where
p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
and prices.id = p2.id
order by sku, p2.valid_from desc, p2.inserted_at desc
)
The table has around 500k rows. This subquery returns around 23k rows. I expect that my above query will delete everything but those 23k rows.
select distinct on (sku) prices.id
from prices
where
valid_to >= CURRENT_DATE
and "type" = 'regular'
order by sku, valid_from desc, inserted_at desc
But it's not deleting anything. Why does it not work the way I expect it to?
CREATE TABLE "public"."prices" (
"id" Bigint DEFAULT nextval('prices_id_seq'::regclass) NOT NULL,
"type" Character Varying( 255 ) NOT NULL,
"unit_price" Numeric( 11, 3 ) NOT NULL,
"wholesale_price" Numeric( 11, 3 ),
"min_wholesale_quantity" Numeric( 11, 3 ),
"valid_from" Date NOT NULL,
"valid_to" Date NOT NULL,
"product_cost" Numeric( 11, 3 ) NOT NULL,
"tax_rate" Numeric( 11, 3 ) NOT NULL,
"store_product_id" Bigint,
"inserted_at" Timestamp Without Time Zone NOT NULL,
"updated_at" Timestamp Without Time Zone NOT NULL,
"sku" Character Varying( 2044 ),
PRIMARY KEY ( "id" ) );
CREATE INDEX "prices_store_product_id_index" ON "public"."prices" USING btree( "store_product_id" Asc NULLS Last );
CREATE INDEX "index_sku" ON "public"."prices" USING btree( "sku" Asc NULLS Last );
ALTER TABLE "public"."prices"
ADD CONSTRAINT "prices_store_product_id_fkey" FOREIGN KEY ( "store_product_id" )
REFERENCES "public"."store_products" ( "id" ) MATCH SIMPLE
ON DELETE Cascade
ON UPDATE No Action;
postgresql delete postgresql-9.5
I want to delete records from a table that are not found in a subquery. But it's not having the desired result.
The table has multiple rows per SKU. Each price row has a valid_from
and valid_to
date fields. I want to erase all prices but keep those that are currently valid (where valid_to
is greater or equal to current date). I want to maintain future prices (where valid_from
is in the future), but one step at a time.
Here's what I tried:
delete from prices
where prices.id <> (
select distinct on (sku) prices.id
from prices p2
where
p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
and prices.id = p2.id
order by sku, p2.valid_from desc, p2.inserted_at desc
)
The table has around 500k rows. This subquery returns around 23k rows. I expect that my above query will delete everything but those 23k rows.
select distinct on (sku) prices.id
from prices
where
valid_to >= CURRENT_DATE
and "type" = 'regular'
order by sku, valid_from desc, inserted_at desc
But it's not deleting anything. Why does it not work the way I expect it to?
CREATE TABLE "public"."prices" (
"id" Bigint DEFAULT nextval('prices_id_seq'::regclass) NOT NULL,
"type" Character Varying( 255 ) NOT NULL,
"unit_price" Numeric( 11, 3 ) NOT NULL,
"wholesale_price" Numeric( 11, 3 ),
"min_wholesale_quantity" Numeric( 11, 3 ),
"valid_from" Date NOT NULL,
"valid_to" Date NOT NULL,
"product_cost" Numeric( 11, 3 ) NOT NULL,
"tax_rate" Numeric( 11, 3 ) NOT NULL,
"store_product_id" Bigint,
"inserted_at" Timestamp Without Time Zone NOT NULL,
"updated_at" Timestamp Without Time Zone NOT NULL,
"sku" Character Varying( 2044 ),
PRIMARY KEY ( "id" ) );
CREATE INDEX "prices_store_product_id_index" ON "public"."prices" USING btree( "store_product_id" Asc NULLS Last );
CREATE INDEX "index_sku" ON "public"."prices" USING btree( "sku" Asc NULLS Last );
ALTER TABLE "public"."prices"
ADD CONSTRAINT "prices_store_product_id_fkey" FOREIGN KEY ( "store_product_id" )
REFERENCES "public"."store_products" ( "id" ) MATCH SIMPLE
ON DELETE Cascade
ON UPDATE No Action;
postgresql delete postgresql-9.5
postgresql delete postgresql-9.5
edited Jul 5 '18 at 23:16
Mohamad
asked Jul 5 '18 at 21:46
MohamadMohamad
1338
1338
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 1 min ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
2
Inside the subquery you require " and prices.id = p2.id", outside you specify "<>". An id can't be equal and different at the same time.
– Gerard H. Pille
Jul 5 '18 at 22:32
Is prices.id not unique?
– Gerard H. Pille
Jul 5 '18 at 23:03
Please Edit your question and add theCREATE TABLE
statements for the tables in question adding the desired output. Those answering need a Minimal, Complete, and Verifiable example. Use formatted text please, no screen shots.
– Evan Carroll
Jul 5 '18 at 23:07
@GerardH.Pille each sku can have multiple prices, wherevalid_from
andvalid_to
is in the past or future. I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those wherevalid_from
is in the future), but one step at a time.
– Mohamad
Jul 5 '18 at 23:12
@EvanCarroll done.
– Mohamad
Jul 5 '18 at 23:12
|
show 3 more comments
2
Inside the subquery you require " and prices.id = p2.id", outside you specify "<>". An id can't be equal and different at the same time.
– Gerard H. Pille
Jul 5 '18 at 22:32
Is prices.id not unique?
– Gerard H. Pille
Jul 5 '18 at 23:03
Please Edit your question and add theCREATE TABLE
statements for the tables in question adding the desired output. Those answering need a Minimal, Complete, and Verifiable example. Use formatted text please, no screen shots.
– Evan Carroll
Jul 5 '18 at 23:07
@GerardH.Pille each sku can have multiple prices, wherevalid_from
andvalid_to
is in the past or future. I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those wherevalid_from
is in the future), but one step at a time.
– Mohamad
Jul 5 '18 at 23:12
@EvanCarroll done.
– Mohamad
Jul 5 '18 at 23:12
2
2
Inside the subquery you require " and prices.id = p2.id", outside you specify "<>". An id can't be equal and different at the same time.
– Gerard H. Pille
Jul 5 '18 at 22:32
Inside the subquery you require " and prices.id = p2.id", outside you specify "<>". An id can't be equal and different at the same time.
– Gerard H. Pille
Jul 5 '18 at 22:32
Is prices.id not unique?
– Gerard H. Pille
Jul 5 '18 at 23:03
Is prices.id not unique?
– Gerard H. Pille
Jul 5 '18 at 23:03
Please Edit your question and add the
CREATE TABLE
statements for the tables in question adding the desired output. Those answering need a Minimal, Complete, and Verifiable example. Use formatted text please, no screen shots.– Evan Carroll
Jul 5 '18 at 23:07
Please Edit your question and add the
CREATE TABLE
statements for the tables in question adding the desired output. Those answering need a Minimal, Complete, and Verifiable example. Use formatted text please, no screen shots.– Evan Carroll
Jul 5 '18 at 23:07
@GerardH.Pille each sku can have multiple prices, where
valid_from
and valid_to
is in the past or future. I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from
is in the future), but one step at a time.– Mohamad
Jul 5 '18 at 23:12
@GerardH.Pille each sku can have multiple prices, where
valid_from
and valid_to
is in the past or future. I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from
is in the future), but one step at a time.– Mohamad
Jul 5 '18 at 23:12
@EvanCarroll done.
– Mohamad
Jul 5 '18 at 23:12
@EvanCarroll done.
– Mohamad
Jul 5 '18 at 23:12
|
show 3 more comments
2 Answers
2
active
oldest
votes
Use the "not in", and don't refer to the outer query:
delete from prices
where (sku,id) Not in (
select p2.sku, p2.id
from prices p2
where p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
)
You're query does not include thedistinct on
clause. I'll try it, but I suspect it might work as intended.
– Mohamad
Jul 5 '18 at 22:43
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
You should really almost never useNOT IN
. In most senses, it's inferior toNOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
|
show 7 more comments
Based on your comment "I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from is in the future), but one step at a time", here are two steps, erase only old prices:
delete from prices
where p2.valid_to < CURRENT_DATE
or p2."type" != 'regular'
If this is too much for a single transaction, add a test on the number of rows (limit, rownum < ...) and repeat until all old prices are gone.
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f211481%2fdelete-records-if-not-present-in-subquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the "not in", and don't refer to the outer query:
delete from prices
where (sku,id) Not in (
select p2.sku, p2.id
from prices p2
where p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
)
You're query does not include thedistinct on
clause. I'll try it, but I suspect it might work as intended.
– Mohamad
Jul 5 '18 at 22:43
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
You should really almost never useNOT IN
. In most senses, it's inferior toNOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
|
show 7 more comments
Use the "not in", and don't refer to the outer query:
delete from prices
where (sku,id) Not in (
select p2.sku, p2.id
from prices p2
where p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
)
You're query does not include thedistinct on
clause. I'll try it, but I suspect it might work as intended.
– Mohamad
Jul 5 '18 at 22:43
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
You should really almost never useNOT IN
. In most senses, it's inferior toNOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
|
show 7 more comments
Use the "not in", and don't refer to the outer query:
delete from prices
where (sku,id) Not in (
select p2.sku, p2.id
from prices p2
where p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
)
Use the "not in", and don't refer to the outer query:
delete from prices
where (sku,id) Not in (
select p2.sku, p2.id
from prices p2
where p2.valid_to >= CURRENT_DATE
and p2."type" = 'regular'
)
edited Jul 5 '18 at 22:53
answered Jul 5 '18 at 22:40
Gerard H. PilleGerard H. Pille
1,296128
1,296128
You're query does not include thedistinct on
clause. I'll try it, but I suspect it might work as intended.
– Mohamad
Jul 5 '18 at 22:43
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
You should really almost never useNOT IN
. In most senses, it's inferior toNOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
|
show 7 more comments
You're query does not include thedistinct on
clause. I'll try it, but I suspect it might work as intended.
– Mohamad
Jul 5 '18 at 22:43
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
You should really almost never useNOT IN
. In most senses, it's inferior toNOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
You're query does not include the
distinct on
clause. I'll try it, but I suspect it might work as intended.– Mohamad
Jul 5 '18 at 22:43
You're query does not include the
distinct on
clause. I'll try it, but I suspect it might work as intended.– Mohamad
Jul 5 '18 at 22:43
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I tried it and it is taking much too long. Around 5 minutes has passed and it's still running.
– Mohamad
Jul 5 '18 at 22:48
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
I'll add the sku, but the query may take a couple of days. Also, the "prices.id" in the subselect was an error.
– Gerard H. Pille
Jul 5 '18 at 22:52
You should really almost never use
NOT IN
. In most senses, it's inferior to NOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
You should really almost never use
NOT IN
. In most senses, it's inferior to NOT EXISTS
– Evan Carroll
Jul 5 '18 at 22:54
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
@EvanCarroll postgresql is wise enough to handle this the best way
– Gerard H. Pille
Jul 5 '18 at 22:56
|
show 7 more comments
Based on your comment "I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from is in the future), but one step at a time", here are two steps, erase only old prices:
delete from prices
where p2.valid_to < CURRENT_DATE
or p2."type" != 'regular'
If this is too much for a single transaction, add a test on the number of rows (limit, rownum < ...) and repeat until all old prices are gone.
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
add a comment |
Based on your comment "I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from is in the future), but one step at a time", here are two steps, erase only old prices:
delete from prices
where p2.valid_to < CURRENT_DATE
or p2."type" != 'regular'
If this is too much for a single transaction, add a test on the number of rows (limit, rownum < ...) and repeat until all old prices are gone.
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
add a comment |
Based on your comment "I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from is in the future), but one step at a time", here are two steps, erase only old prices:
delete from prices
where p2.valid_to < CURRENT_DATE
or p2."type" != 'regular'
If this is too much for a single transaction, add a test on the number of rows (limit, rownum < ...) and repeat until all old prices are gone.
Based on your comment "I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those where valid_from is in the future), but one step at a time", here are two steps, erase only old prices:
delete from prices
where p2.valid_to < CURRENT_DATE
or p2."type" != 'regular'
If this is too much for a single transaction, add a test on the number of rows (limit, rownum < ...) and repeat until all old prices are gone.
answered Jul 6 '18 at 5:34
Gerard H. PilleGerard H. Pille
1,296128
1,296128
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
add a comment |
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
That won’t work either. I think sample data would help. When I get to the office I will add some. But many prices are valid until 01-01-9999. So an SKU can have multiple valid prices separated only by their ‘valid_from’.
– Mohamad
Jul 6 '18 at 10:51
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f211481%2fdelete-records-if-not-present-in-subquery%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
2
Inside the subquery you require " and prices.id = p2.id", outside you specify "<>". An id can't be equal and different at the same time.
– Gerard H. Pille
Jul 5 '18 at 22:32
Is prices.id not unique?
– Gerard H. Pille
Jul 5 '18 at 23:03
Please Edit your question and add the
CREATE TABLE
statements for the tables in question adding the desired output. Those answering need a Minimal, Complete, and Verifiable example. Use formatted text please, no screen shots.– Evan Carroll
Jul 5 '18 at 23:07
@GerardH.Pille each sku can have multiple prices, where
valid_from
andvalid_to
is in the past or future. I want to erase all prices and only leave those currently valid. I know my query will also erase future prices (those wherevalid_from
is in the future), but one step at a time.– Mohamad
Jul 5 '18 at 23:12
@EvanCarroll done.
– Mohamad
Jul 5 '18 at 23:12