-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeduplicate_SQL.py
More file actions
42 lines (31 loc) · 1 KB
/
Copy pathdeduplicate_SQL.py
File metadata and controls
42 lines (31 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Find duplicate records and remove them from cb_news
# https://support.microsoft.com/en-us/kb/139444
# Make a copy of cb_news in case something goes wrong?
SELECT *
INTO cb_news_backup
FROM cb_news;
# identify duplicates (not just uuid duplicates but uuid and company duplicates)
SELECT uuid, company, count(*)
FROM cb_news
GROUP BY uuid, company
HAVING count(*) > 1;
# select duplicate key-values into a holding table called 'holdkey'
SELECT uuid, company, count(*)
INTO holdkey
FROM cb_news
GROUP BY uuid, company
HAVING count(*) > 1
# Select the duplicate rows into a holding table, holddups
# (eliminating duplicates in the process)
SELECT DISTINCT cb_news.*
INTO holddups
FROM cb_news, holdkey
WHERE cb_news.uuid = holdkey.uuid
AND cb_news.company = holdkey.company
# Delete the duplicate rows from the original table
DELETE cb_news
FROM cb_news, holdkey
WHERE cb_news.uuid = holdkey.uuid
AND cb_news.company = holdkey.company
# Put the unique rows back into the table
INSERT cb_news SELECT * FROM holddups