-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup_duplicate_practice_tests.sql
More file actions
39 lines (34 loc) · 1.1 KB
/
cleanup_duplicate_practice_tests.sql
File metadata and controls
39 lines (34 loc) · 1.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
-- Script to clean up duplicate practice tests in the practice_assessments table
-- This script will identify and remove duplicate entries while keeping one entry for each unique student-assessment pair
-- First, let's see how many duplicates we have
SELECT
student_id,
assessment_id,
COUNT(*) as count
FROM
practice_assessments
GROUP BY
student_id, assessment_id
HAVING
COUNT(*) > 1;
-- Create a temporary table to store the practice_ids we want to keep
CREATE TEMPORARY TABLE practice_ids_to_keep AS
SELECT MIN(practice_id) as practice_id
FROM practice_assessments
GROUP BY student_id, assessment_id;
-- Delete all practice tests that aren't in our "to keep" list
DELETE FROM practice_assessments
WHERE practice_id NOT IN (SELECT practice_id FROM practice_ids_to_keep);
-- Drop our temporary table
DROP TEMPORARY TABLE practice_ids_to_keep;
-- Verify the cleanup - this should return no rows if all duplicates were removed
SELECT
student_id,
assessment_id,
COUNT(*) as count
FROM
practice_assessments
GROUP BY
student_id, assessment_id
HAVING
COUNT(*) > 1;