Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
help required to wrire a query.

Roy4321
Member Posts: 305 Bronze Badge
Hi,
I have the below data
TableA
name----id----status
mat----123---- 1
mat----123---- 2 -- to be deleted
mat----123---- 2 -- to be deleted
sam----456---- 2
sam----456---- 2 -- to be deleted
dan----789---- 1
dan----789---- 1 -- to be deleted
dan----789---- 2 -- to be deleted
pan----111---- 1
can someone give me an idea how to write a query that delletes the records marked as "-- to be deleted"
Thanks,
Mathew
Edited by: [email protected] on Dec 4, 2009 12:21 AM
I have the below data
TableA
name----id----status
mat----123---- 1
mat----123---- 2 -- to be deleted
mat----123---- 2 -- to be deleted
sam----456---- 2
sam----456---- 2 -- to be deleted
dan----789---- 1
dan----789---- 1 -- to be deleted
dan----789---- 2 -- to be deleted
pan----111---- 1
can someone give me an idea how to write a query that delletes the records marked as "-- to be deleted"
Thanks,
Mathew
Edited by: [email protected] on Dec 4, 2009 12:21 AM
Answers
-
DELETE FROM table_name t1 WHERE (EXISTS (SELECT ROWID FROM table_name t2 WHERE t1.name = t2.name AND t1.id = t2.id AND t1.status = t2.status AND t2.ROWID > t1.ROWID)) DELETE FROM table_name t1 WHERE (EXISTS (SELECT ROWID FROM table_name t2 WHERE t1.name = t2.name AND t1.id = t2.id AND t1.status <> '1' AND t2.ROWID > t1.ROWID))
Regards,
Mahesh Kaila -
What is the logic behind this? Why are those records identified as to be deleted?
-
I think this might work
delete from t where rowid not in (select min(rowid) from t group by name,id)
-
Those are actually duplicate records ...only it differs in status...but if there is a record with status as 1 i want to keep that record and delete the rest..but if records are only present with status 2 then the first record with status 2 needs to be deleted --these are for each ID.
hope this helps!! -
this will not work...as i will need to keep the record with status as 1 if it exists.
-
something like?
SQL> create table test 2 as 3 select 'mat' name, 123 id, 1 status from dual union all 4 select 'mat' name, 123 id, 2 status from dual union all -- to be deleted 5 select 'mat' name, 123 id, 2 status from dual union all -- to be deleted 6 select 'sam' name, 456 id, 2 status from dual union all 7 select 'sam' name, 456 id, 2 status from dual union all -- to be deleted 8 select 'dan' name, 789 id, 1 status from dual union all 9 select 'dan' name, 789 id, 1 status from dual union all -- to be deleted 10 select 'dan' name, 789 id, 2 status from dual union all -- to be deleted 11 select 'pan' name, 111 id, 1 status from dual 12 / Table created. SQL> SQL> delete from test 2 where rowid in 3 (select rid 4 from ( 5 select rowid rid 6 , row_number() over (partition by name, id 7 order by status 8 ) rn 9 from test 10 ) 11 where rn > 1 12 ) 13 / 5 rows deleted. SQL> SQL> select * 2 from test 3 / NAM ID STATUS --- ---------- ---------- mat 123 1 sam 456 2 dan 789 1 pan 111 1 SQL> SQL> drop table test purge 2 / Table dropped. SQL>
-
then what about this row that you mentioned
sam----456---- 2 sam----456---- 2 -- to be deleted
-
Thanks Alex...that works!!
-
if i am correct the query you wrote will take the min rowid and delete the rest..but if min(rowid) is not for status 1 then this gets deleted too...
-
create table test2 as (select 'mat' name, 123 id, 1 status from dual union all select 'mat' name, 123 id, 2 status from dual union all -- to be deleted select 'mat' name, 123 id, 2 status from dual union all -- to be deleted select 'sam' name, 456 id, 2 status from dual union all select 'sam' name, 456 id, 2 status from dual union all -- to be deleted select 'dan' name, 789 id, 1 status from dual union all select 'dan' name, 789 id, 1 status from dual union all -- to be deleted select 'dan' name, 789 id, 2 status from dual union all -- to be deleted select 'pan' name, 111 id, 1 status from dual ); select * from test2; NAME ID STATUS mat 123 1 mat 123 2 mat 123 2 sam 456 2 sam 456 2 dan 789 1 dan 789 1 dan 789 2 pan 111 1 delete from test2 where rowid not in (select min(rowid) from test2 group by name,id); 5 rows deleted select * from test2; NAME ID STATUS mat 123 1 sam 456 2 dan 789 1 pan 111 1
This discussion has been closed.