-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path37. AK-How to Delete Duplicates in Production Environment.sql
More file actions
60 lines (53 loc) · 1.99 KB
/
37. AK-How to Delete Duplicates in Production Environment.sql
File metadata and controls
60 lines (53 loc) · 1.99 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
create table transaction (
order_id int,
order_date date,
product_name varchar(6),
order_amount int,
create_time datetime
);
#drop table transaction;
select * from transaction;
insert into transaction values(1,'2022-03-03','P1',150,CURRENT_TIMESTAMP());
insert into transaction values(2,'2022-03-03','P2',200,CURRENT_TIMESTAMP());
insert into transaction values(3,'2022-03-03','P3',300,CURRENT_TIMESTAMP());
select * from transaction;
#step 1 - take backup of original table
##ssms
select * into transaction_table_backup from transaction;
#step 2 - delet duplicates,using delete
select order_id,
min(create_time) as no_of_records from transaction
group by order_id having count(1)>1;
#below queery not work in ssms
delete from transaction where order_id,create_time in (select order_id,
min(create_time) from transaction
group by order_id having count(1)>1;
#so use this ,if more than 2 duplicate need to run 1 more time this querry
delete t from transaction t inner join
(select order_id,
min(create_time) as create_time from transaction
group by order_id having count(1)>1) as a
on a.order_id=t.order_id and
a.create_time=t.create_time;
#second approcah by using row number,if pk is two coln,put 2 coln in partition by
select * ,
row_number() over(partition by order_id order by create_time desc) as rn
from transaction;
#insert into row number 1,take backup before and use back up table below,insert into original
insert into transaction
select order_id,order_date,product_name,order_time,create_time from
(
select * ,
row_number() over(partition by order_id order by create_time desc) as rn
from transaction) a
where rn=1;
#delete pure duplicates,all same
#update transaction set create_time=CURRENT_TIMESTAMP();
#take back up first, then insert into originak table by distinct,above querry will not work ,but below querry work in all cases
select distinct * from transaction
#take backup
insert into transaction_backup
select * from transaction;
#insert
insert into transaction
select distinct * from transaction_backup;