-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicate_detection.sql
More file actions
76 lines (65 loc) · 1.98 KB
/
Copy pathduplicate_detection.sql
File metadata and controls
76 lines (65 loc) · 1.98 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- ============================================================
-- Script : duplicate_detection.sql
-- Purpose : Identify duplicate records in source and target
-- Author : Arunkumar Aravindhakshan
-- Tools : Snowflake / AWS Redshift / Oracle
-- ============================================================
-- -------------------------------------------------------
-- 1. SIMPLE DUPLICATE CHECK ON PRIMARY KEY
-- -------------------------------------------------------
SELECT
claim_id,
COUNT(*) AS duplicate_count
FROM <target_schema>.claims_target
GROUP BY claim_id
HAVING COUNT(*) > 1
ORDER BY duplicate_count DESC;
-- -------------------------------------------------------
-- 2. FULL DUPLICATE ROWS (ALL COLUMNS IDENTICAL)
-- -------------------------------------------------------
SELECT
claim_id,
patient_id,
claim_amount,
admission_date,
COUNT(*) AS row_count
FROM <target_schema>.claims_target
GROUP BY
claim_id,
patient_id,
claim_amount,
admission_date
HAVING COUNT(*) > 1;
-- -------------------------------------------------------
-- 3. DUPLICATE DETECTION USING WINDOW FUNCTION
-- Tags each duplicate with a row number — keeps row 1, flags rest
-- -------------------------------------------------------
WITH ranked_records AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY claim_id
ORDER BY load_timestamp DESC
) AS row_num
FROM <target_schema>.claims_target
)
SELECT *
FROM ranked_records
WHERE row_num > 1;
-- -------------------------------------------------------
-- 4. DUPLICATE SUMMARY REPORT
-- -------------------------------------------------------
SELECT
'Total Records' AS metric,
COUNT(*) AS value
FROM <target_schema>.claims_target
UNION ALL
SELECT
'Unique claim_ids',
COUNT(DISTINCT claim_id)
FROM <target_schema>.claims_target
UNION ALL
SELECT
'Duplicate Records',
COUNT(*) - COUNT(DISTINCT claim_id)
FROM <target_schema>.claims_target;