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

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

Related

Deleting completely identical duplicates from db

We have a table in our db with copied data that has completely duplicated many rows. Because the id is also duplicated there is nothing we can use to select just the duplicates. I tried using a limit to only delete 1 but redshift gave a syntax error when trying to use limit.
Any ideas how we can delete just one of two rows that have completely identical information?
Use select distinct to create a new table. Then either truncate & copy the data, or drop the original table and rename the new table to the original name:
create table t2 as select distinct * from t;
truncate t;
insert into t from select * from t2;
drop table t2;
Add column a column with unique values. identity(seed, step) looks interesting.

Hive: Removing Duplicate Rows from Table

I have a table which contains millions of records and all the records have duplicates. So I am trying to extract all the distinct rows in the table.
Here's the query I am using:
CREATE TABLE unique_table AS SELECT DISTINCT * FROM duplicates_table;
Is this the efficient way to do this job? Or is there a way to remove duplicate rows without creating a new table?
You can use the same table:
INSERT OVERWRITE table_name SELECT DISTINCT * FROM table_name;

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.

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