delete duplicate records except one [duplicate] - sql

This question already has answers here:
Removing duplicate rows from table in Oracle
(24 answers)
How do I find duplicate values in a table in Oracle?
(13 answers)
Closed 6 years ago.
I have a replication of around 200 records in a table, I want to remove all of then except one, how can I do this ??

Source http://www.devx.com
It's easy to introduce duplicate rows of data into Oracle tables by
running a data load twice without the primary key or unique indexes
created or enabled.Here column1, column2, column3 constitute the identifying key for each record.
DELETE FROM our_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM our_table
GROUP BY column1, column2, column3) ;

use the following query. This will be applicable if there is an Id available for the table.
delete from tableA where id in(select top 199 id from tableA)

Related

How to remove duplicate rows in a SQL Server table [duplicate]

This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 4 years ago.
I have a SQL Server table with ~100 columns including the columns Id and CreationDate. Due to a bad constraint in its initial design, there are now many duplicate rows (i.e. rows whose values are identical across ALL columns).
Can you suggest a script to remove those duplicate rows?
Also, what would be a script to select all distinct Ids with the latest CreationDate?
Thanks
You can use the following script to remove duplicate rows from a Microsoft SQL Server table:
SELECT DISTINCT *
INTO duplicate_table
FROM original_table
GROUP BY key_value
HAVING COUNT(key_value) > 1
DELETE original_table
WHERE key_value
IN (SELECT key_value
FROM duplicate_table)
INSERT original_table
SELECT *
FROM duplicate_table
DROP TABLE duplicate_table
When this script is executed, it follows these steps:
It moves one instance of any duplicate row in the original table to a duplicate table.
It deletes all rows from the original table that also reside in the duplicate table.
It moves the rows in the duplicate table back into the original table.
It drops the duplicate table.

Duplicated rows in MS Access database [duplicate]

This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 9 years ago.
MS Access database was corrupted and in one table few rows was duplicated. They are absolutely same and there isn't any unique field between duplicates, even primary keys. Because of this, primary key was reseted from this table after repairing database.
Now I can only know rows that were duplicated:
select * from tablename
where id in(
select id from tablename
group by id
having count (*) > 1)
To designate primary key I must delete one of two duplicates, but don't know how.
One way you can do this is with a temporary table:
select distinct t.*
into TempTABLE
from tablename t;
delete from tablename;
insert into tablename
select *
from TempTable;
That is, remove the duplicates using distinct. Delete all the rows from the original table, and then insert the unique rows.

Insert/Update functionality in postgresql [duplicate]

This question already has answers here:
Insert, on duplicate update in PostgreSQL?
(18 answers)
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
(7 answers)
Closed 9 years ago.
i'm manually updating table in my DB using import functionality in PostgreSQL. Working with large number of data i came across an issue of duplicating primary keys. I am looking for a script to upload only values that do not violate primary key assumption, and those that to violate are to be ignored (not uploaded or updated).
I have already seen a code that would kind of do what i need however not quite sure if it will work for me.
Columns i am working with are:
acc_ph_id (primary_key);acc_ph_no;ph_type;ph_flag
Any suggestions will be highly appreciated as i am rather new to Postgresql in general.
Upload the table into a staging table with no constraints.
Then load the table into the full table eliminating duplicates:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table
order by acc_ph_id;
EDIT:
Oh, if the problem is the keys that already exist, then do:
insert into real_table(acc_ph_id, acc_ph_no, ph_type, ph_flag)
select distinct on (acc_ph_id) acc_ph_id, acc_ph_no, ph_type, ph_flag
from staging_table st
where not exists (select 1
from real_table rt
where rt.acc_ph_id = st.acc_ph_id
)
order by acc_ph_id;

How I can delete repeated rows from database? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Remove duplicates in large MySql table
Can I extract the extract records that are duplicated in sql?
How can I delete duplicate rows in a table
I need something to delete repeated rows from the database.
I found out how many rows are repeated in table using this query :
SELECT GoodCode FROM Good_
and here is distinct query SELECT Distinct GoodCode FROM Good_
The second one has lower records. Please guide me how I can delete repeated rows from the first one.
Simple method:
SELECT DISTINCT *
INTO #TempGood
FROM Good_
TRUNCATE TABLE Good_
INSERT Good_
SELECT *
FROM #TempGood
DROP TABLE #TempGood
create table temptable as select distinct * from Good_;
drop table Good_;
rename temptable to Good_;

Move table row to a new table

In SQLite is it possible to move a whole row of a table to another table in one query, rather than selecting the row then inserting it into another table then deleting the row in the original table (3 query's)
well i am not sure about doing all 3 in one query but you can do the below for select and insert
Select column1, column2 into tableName
and then your delete