-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSQLRemoveDuplicatesWithLeftOuterJoin.sql
49 lines (39 loc) · 1.19 KB
/
TSQLRemoveDuplicatesWithLeftOuterJoin.sql
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
/* LEFT OUTER JOIN to remove duplicates*/
-- Build experiment table
DECLARE @dup TABLE(
ColumnOne VARCHAR(1),
ColumnTwo INT
)
-- Insert experiment values
INSERT INTO @dup VALUES('A',1)
INSERT INTO @dup VALUES('A',2)
INSERT INTO @dup VALUES('B',3)
INSERT INTO @dup VALUES('C',4)
INSERT INTO @dup VALUES('D',5)
INSERT INTO @dup VALUES('E',6)
INSERT INTO @dup VALUES('E',7)
INSERT INTO @dup VALUES('E',8)
INSERT INTO @dup VALUES('E',9)
-- See the duplicates
SELECT *
FROM @dup
-- What should exist in the table
SELECT ColumnOne, ColumnTwo = MIN(ColumnTwo)
FROM @dup
GROUP BY ColumnOne
-- What should be removed
SELECT *
FROM @dup A
LEFT OUTER JOIN (SELECT ColumnOne, ColumnTwo = MIN(ColumnTwo) FROM @dup GROUP BY ColumnOne) B ON A.ColumnTwo = B.ColumnTwo
WHERE B.ColumnTwo IS NULL
-- Remove the duplicates
DELETE @dup
FROM @dup A
LEFT OUTER JOIN (SELECT ColumnOne, ColumnTwo = MIN(ColumnTwo) FROM @dup GROUP BY ColumnOne) B ON A.ColumnTwo = B.ColumnTwo
WHERE B.ColumnTwo IS NULL
-- See the results
SELECT *
FROM @dup
/*
Note: numerous ways to remove duplicates exist (such as the CTE), but these methods, including the CTE, show fast and effective methods.
*/