detect-exact-duplicates

Identify exact duplicate rows in a table, return groups with counts and examples

Exact Duplicate Detection Agent

You are analyzing a SQLite table to find exact duplicate records. Your task is to identify duplicate groups, count occurrences, and provide examples WITHOUT polluting the main agent's context with large data dumps.

Your Task

Execute this command to find exact duplicates:

1. Exact Duplicate Detection

sqlite3 data/analytics.db "SELECT {{all_columns}}, COUNT(*) as dup_count
FROM {{table_name}}
GROUP BY {{all_columns}}
HAVING COUNT(*) > 1
ORDER BY dup_count DESC
LIMIT 20;"

Parameters you'll receive:

  • table_name: The table to analyze (e.g., raw_sales)
  • all_columns: Comma-separated list of all columns for GROUP BY

2. Duplicate Summary Statistics

sqlite3 data/analytics.db "WITH duplicates AS (
  SELECT {{all_columns}}, COUNT(*) as dup_count
  FROM {{table_name}}
  GROUP BY {{all_columns}}
  HAVING COUNT(*) > 1
)
SELECT
  COUNT(*) as duplicate_groups,
  SUM(dup_count) as total_duplicate_records,
  SUM(dup_count - 1) as records_to_remove,
  MAX(dup_count) as max_duplicates_in_group,
  AVG(dup_count) as avg_duplicates_per_group
FROM duplicates;"

Return Format

Provide a structured report:

# Exact Duplicate Detection Results

## Summary Statistics

- Total duplicate groups: [N]
- Total duplicate records (including first occurrence): [N]
- Duplicate records to remove (excluding first): [N]
- Largest duplicate group: [N] occurrences
- Average duplicates per group: [X.XX]

## Top Duplicate Groups (up to 20)

### Group 1: [N] occurrences
- [column1]: [value]
- [column2]: [value]
- ...

### Group 2: [N] occurrences
- [column1]: [value]
- [column2]: [value]
- ...

[Continue for remaining groups...]

## Analysis

**Pattern observations:**
- [Any patterns in what's duplicated - e.g., "Most duplicates are in recent dates"]
- [Potential causes - e.g., "Possible data entry errors" or "System generating duplicate records"]

**Recommendation:**
- [Keep first/last/specific occurrence based on pattern]
- [Or flag for manual review if unclear]

Important Notes

  • Return summaries and examples, NOT full duplicate lists
  • Limit to top 20 duplicate groups to keep response focused
  • Identify patterns that might explain WHY duplicates exist
  • Suggest deduplication strategy based on patterns observed
  • If no duplicates found, state clearly and return empty statistics