Home » SQL & PL/SQL » SQL & PL/SQL » How to delete records found with MINUS
|
|
|
|
|
|
|
Re: How to delete records found with MINUS [message #484624 is a reply to message #484566] |
Thu, 02 December 2010 02:05   |
msmallya77
Messages: 28 Registered: June 2009 Location: Ahmedabad
|
Junior Member |
|
|
Hi,
Quote:I wish to delete records found with function MINUS from file1, which is a result of records not found in file2
converting above is,
confirm your result set before deleting records as
select * from file1 a
where not exists (select * from file2 b where a.key=b.key)
Then delete records as
delete * from file1 a
where not exists (select * from file2 b where a.key=b.key)
Regards,
MSMallya
|
|
|
|
|
Re: How to delete records found with MINUS [message #484662 is a reply to message #484625] |
Thu, 02 December 2010 04:42   |
 |
kingman
Messages: 19 Registered: December 2010
|
Junior Member |
|
|
Michel Cadot wrote on Thu, 02 December 2010 09:42How does this answer the question? Where is MINUS in your statements?
Regards
Michel
It would be be nicer if the MINUS function is able to returns the rownumbers as well.
That way I don't have to go trough the tabel again.
I used the concatenate function to compare nearly all columns in both tables. Now I have to make sure that the combination of specified columns is unique. Otherwise i would be deleting the wrong records.
[Updated on: Thu, 02 December 2010 04:50] Report message to a moderator
|
|
|
|
Re: How to delete records found with MINUS [message #484673 is a reply to message #484662] |
Thu, 02 December 2010 04:53   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
DELETE
FROM file1 y
WHERE EXIST (SELECT *
FROM (SELECT a.col1, a.col2, a.col3, a.col4
FROM file1 a
MINUS
SELECT b.col1, b.col2, b.col3, b.col4
FROM file2 b
) z
WHERE z.col1 = y.col1
AND z.col2 = y.col2
AND z.col3 = y.col3
AND (z.col4 = y.col4
OR (z.col4 IS NULL AND y.col4 IS NULL ))
|
|
|
Re: How to delete records found with MINUS [message #484675 is a reply to message #484673] |
Thu, 02 December 2010 04:57   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
DELETE
FROM file1
WHERE rowid in
(SELECT rowid
FROM file1 a
WHERE (a.col1, a.col2, a.col3, a.col4) IN
(SELECT a.col1, a.col2, a.col3, a.col4
FROM file1 a
MINUS
SELECT b.col1, b.col2, b.col3, b.col4
FROM file2 b
)
/
Regards
Michel
|
|
|
|
Re: How to delete records found with MINUS [message #484678 is a reply to message #484677] |
Thu, 02 December 2010 05:08   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
10g is NOT a version, it is a marketing name.
10.2.0.4.0 is a version number.
You forgot to read:
Quote:Please realize that we don't have your tables & we don't have your data.
Therefore we can't run, test or improve your posted SQL.
It would be helpful if you provided DDL (CREATE TABLE ...) for tables involved.
It would be helpful if you provided DML (INSERT INTO ...) for test data.
So is your problem, "how to express the DELETE statement with MINUS?" or "how to get delete faster?"?
If you don't post your actual question, you will not have the answer for it.
Regards
Michel
[Updated on: Thu, 02 December 2010 05:09] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
Re: How to delete records found with MINUS [message #484699 is a reply to message #484694] |
Thu, 02 December 2010 05:51   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
kingman wrote on Thu, 02 December 2010 11:38sorry about that.
i tried to tell you that is was a type error, missed the bracket in my message but not in TOAD. (That's where i fire my queries)
The query I originally posted is missing a bracket. So if you typo'd it, it's presumably missing two. Take mine, add a ) at the end.
If that doesn't work, run the query in sqlplus (if you've got toad you've got sqlplus) and copy and paste the full execution here. SQLplus is a lot better at highlighting the source of errors than TOAD is.
|
|
|
|
Re: How to delete records found with MINUS [message #484702 is a reply to message #484696] |
Thu, 02 December 2010 05:52   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
kingman wrote on Thu, 02 December 2010 11:41
The problem is that I'm using internet via a citrix farm (data is not accessible from this citrix server) and company regulation does not allow code or data to be send out.
So post dummy tables and dummy data. We probably only need a small handful of lines to recreate your exact scenario.
|
|
|
|
Re: How to delete records found with MINUS [message #484719 is a reply to message #484706] |
Thu, 02 December 2010 07:09   |
 |
kingman
Messages: 19 Registered: December 2010
|
Junior Member |
|
|
table static_test
col1 VARCHAR2(64 BYTE),
col2 VARCHAR2(32 BYTE),
col3 VARCHAR2(64 BYTE),
col4 VARCHAR2(128 BYTE),
col5 VARCHAR2(64 BYTE),
col6 DATE DEFAULT NULL,
col7 DATE
table static_test
COL1;COL2;COL3;COL4;COL5;COL6;COL7
serverx;domaina;AMS;incl;;2-12-2010 13:47:13;2-12-2010 13:47:18;
serverx;domaina;NY;excl;;2-12-2010 13:47:13;2-12-2010 13:47:18;
serverz;domainb;HK;incl;xtra1, xtra2;2-12-2010 13:47:13;2-12-2010 13:47:18;
servery;domaina;BXL;incl;;2-12-2010 13:47:13;2-12-2010 13:49:01;
table auto_test
col1 VARCHAR2(64 BYTE),
col2 VARCHAR2(32 BYTE),
col3 VARCHAR2(64 BYTE),
col4 VARCHAR2(128 BYTE),
col5 VARCHAR2(64 BYTE),
col6 DATE DEFAULT SYSDATE
table auto_test
COL1;COL2;COL3;COL4;COL5;COL6
serverx;domaina;AMS;incl;;2-12-2010 13:47:13;
serverx;domaina;NY;excl;;2-12-2010 13:47:13;
serverz;domainb;HK;incl;xtra1, xtra2;2-12-2010 13:47:13;
select *
FROM STATIC_test Y
WHERE EXISTS
(SELECT *
FROM (
select b.col1, b.col2, b.col3, b.col4, b.col5, b.col6
from static_test b
minus
select a.col1, a.col2, a.col3, a.col4, a.col5, a.col6
from auto_test a
) Z
WHERE Z.col1 = y.col1 AND
z.col2 = Y.col2 AND
z.col3 = Y.col3 AND
Z.col4 = y.col4 AND
Z.col6 = y.col6
-- (z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
);
|
|
|
Re: How to delete records found with MINUS [message #484720 is a reply to message #484675] |
Thu, 02 December 2010 07:15   |
 |
kingman
Messages: 19 Registered: December 2010
|
Junior Member |
|
|
Michel Cadot wrote on Thu, 02 December 2010 11:57DELETE
FROM file1
WHERE rowid in
(SELECT rowid
FROM file1 a
WHERE (a.col1, a.col2, a.col3, a.col4) IN
(SELECT a.col1, a.col2, a.col3, a.col4
FROM file1 a
MINUS
SELECT b.col1, b.col2, b.col3, b.col4
FROM file2 b
)
/
Regards
Michel
No record found with query
select *
FROM static_test
WHERE rowid in
(SELECT rowid
FROM static_test a
WHERE (a.col1, a.col2, a.col3, a.col4, a.col5, a.col6) IN
(
select a.col1, a.col2, a.col3, a.col4, a.col5, a.col6
from static_test a
minus
select b.col1, b.col2, b.col3, b.col4, b.col5, b.col6
from auto_test b
)
);
|
|
|
|
Re: How to delete records found with MINUS [message #484724 is a reply to message #484723] |
Thu, 02 December 2010 07:43   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
-- (z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
^ typo ^2nd typo
Can you please go and read the forum guide, especially the section on how to format your post, and then follow it.
Can you not see how much easier it is to read our queries compared to yours?
EDIT: spotted 2nd typo.
[Updated on: Thu, 02 December 2010 07:44] Report message to a moderator
|
|
|
Re: How to delete records found with MINUS [message #484729 is a reply to message #484699] |
Thu, 02 December 2010 08:09   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
kingman wrote on Thu, 02 December 2010 13:37i commented this line as it's returning an error 'invalid relational operator' (SQL Plus aswell0
I'm very certain that sqlplus gave the same error. I'd be very concerned if it didn't. The reason I said:
cookiemonster wrote on Thu, 02 December 2010 11:51If that doesn't work, run the query in sqlplus (if you've got toad you've got sqlplus) and copy and paste the full execution here. SQLplus is a lot better at highlighting the source of errors than TOAD is.
Is that if you had we would be looking at something like this:
SQL> SELECT 1 b FROM dual d
2 WHERE 'A' IN (SELECT 'A' a FROM dual t WHERE t,a = d.b);
WHERE 'A' IN (SELECT 'A' a FROM dual t WHERE t,a = d.b)
*
ERROR at line 2:
ORA-00920: invalid relational operator
See where the * is? Right below the typo.
We don't ask you do things like that (or to post test cases) to make your life difficult. We ask so that we can get enough information to recreate and/or diagnose your problem so that we can help you fix it.
You appear to be rushing, and it's proving counter-productive. Slow down. Check your queries carefully.
When you've got a nested query that doesn't return the right data break it down. Run the inner select stand alone and see if it's right, then re-add layers until it breaks (I suspect nulls are the culprit).
Also bear in mind that your test case doesn't contain any actual inserts or create tables. You'll generally get better and faster replies if we don't have to do significant editing to recreate your data. Some of us are fitting this in round our day jobs.
|
|
|
Re: How to delete records found with MINUS [message #484736 is a reply to message #484729] |
Thu, 02 December 2010 08:25   |
 |
kingman
Messages: 19 Registered: December 2010
|
Junior Member |
|
|
First of all I'm very sorry about not getting the info right away..
I'm just starting, and I haven't had a decent course in sql....
I overlooked the extra info. Yes I see the *, it's right under the = sign
(z,col5 = Y.Ocol5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
*
I don't know how to get the source to create the tabels and data..
[Updated on: Thu, 02 December 2010 08:25] Report message to a moderator
|
|
|
|
Re: How to delete records found with MINUS [message #484741 is a reply to message #484740] |
Thu, 02 December 2010 08:39   |
 |
kingman
Messages: 19 Registered: December 2010
|
Junior Member |
|
|
I found the typo (comma which should have been a dot). Query runs fine.
However the script suddenly runs a lot longer. 1.46 minutes compared to less than one second with extra check on column 5. But ofcourse still a lot better than the 15-20 minutes with concatenate function.
select *
FROM STATIC_test Y
WHERE EXISTS
(SELECT *
FROM (
select b.col1, b.col2, b.col3, b.col4, b.col5, b.col6
from static_test b
minus
select a.col1, a.col2, a.col3, a.col4, a.col5, a.col6
from auto_test a
) Z
WHERE Z.col1 = y.col1 AND
z.col2 = Y.col2 AND
z.col3 = Y.col3 AND
Z.col4 = y.col4 AND
Z.col6 = y.col6 AND
(z.col5 = Y.col5 OR (Z.col5 IS NULL AND Y.col5 IS NULL))
);
[Updated on: Thu, 02 December 2010 08:44] Report message to a moderator
|
|
|
|
Re: How to delete records found with MINUS [message #484836 is a reply to message #484625] |
Fri, 03 December 2010 01:26   |
msmallya77
Messages: 28 Registered: June 2009 Location: Ahmedabad
|
Junior Member |
|
|
Hi,
Quote:How does this answer the question? Where is MINUS in your statements?
Results are same. And Hence no need to use minus qry.
SO, MINUS Query can be avoided.
Here it is
drop table file1;
drop table file2;
create table file1 (col1 char(5), col2 number(3));
insert into file1 values('STATE', 2);
insert into file1 values('STATA', 2);
insert into file1 values('STATS', 2);
insert into file1 values('ESTAT', 2);
insert into file1 values('TASTE', 2);
insert into file1 values('EAAST', 2);
insert into file1 values('SEATS', 2);
insert into file1 values('ESATA', 2);
insert into file1 values('ETATE', 2);
create table file2 (col1 char(5), col2 number(3));
insert into file2 values('GUEST', 2);
insert into file2 values('GATES', 2);
insert into file2 values('STAGE', 2);
insert into file2 values('EAAST', 2);
insert into file2 values('SEATS', 2);
insert into file2 values('ESATA', 2);
insert into file2 values('ETATE', 2);
commit;
select * from file1 a
where not exists (select * from file2 b where a.col1=b.col1)
order by col1;
COL1 COL2
----- ----------
ESTAT 2
STATA 2
STATE 2
STATS 2
TASTE 2
5 rows selected.
delete from file1 a
where not exists (select * from file2 b where a.col1=b.col1);
5 rows deleted.
rollback;
select * from file1 a
where exists (
select * from file1 b where a.col1=b.col1
minus
select * from file2 b where a.col1=b.col1)
order by col1;
COL1 COL2
----- ----------
ESTAT 2
STATA 2
STATE 2
STATS 2
TASTE 2
5 rows selected.
delete from file1 a
where exists (
select * from file1 b where a.col1=b.col1
minus
select * from file2 b where a.col1=b.col1);
5 rows deleted.
Am I correct? Hope So.
Regards,
MSMallya
|
|
|
Re: How to delete records found with MINUS [message #484838 is a reply to message #484836] |
Fri, 03 December 2010 01:35  |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:Results are same. And Hence no need to use minus qry.
SO, MINUS Query can be avoided.
It does not matter the question was: WITH MINUS.
Note that your statements do not handle NULL.
Regards
Michel
|
|
|
Goto Forum:
Current Time: Tue Aug 05 20:05:44 CDT 2025
|