Skip to content

Commit 996271f

Browse files
authored
Create Data-fix: Removing Duplicate scheduled Supervisor home visits for both Child and Pregnancy.sql
1 parent a3daa44 commit 996271f

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
--------------------------------------------------------------------
2+
-- 1. Removing Duplicate Supervisor Child Home visit
3+
-- Select Query
4+
5+
SET ROLE apfodisha;
6+
7+
WITH duplicate_encounters AS (
8+
SELECT
9+
pe.id,
10+
pe.individual_id,
11+
pe.program_enrolment_id,
12+
pe.created_date_time,
13+
ROW_NUMBER() OVER(PARTITION BY pe.individual_id, pe.program_enrolment_id, pe.encounter_type_id ORDER BY pe.created_date_time DESC NULLS LAST) AS latest
14+
FROM
15+
program_encounter pe
16+
JOIN program_enrolment pe2 ON
17+
pe2.id = pe.program_enrolment_id
18+
AND pe.is_voided IS FALSE
19+
JOIN program p ON
20+
p.id = pe2.program_id
21+
AND p.is_voided IS FALSE
22+
AND p.name = 'Child'
23+
JOIN encounter_type et ON
24+
et.id = pe.encounter_type_id
25+
AND et.name = 'Supervisor Child Home visit'
26+
WHERE
27+
pe.encounter_date_time IS NULL
28+
AND pe.cancel_date_time IS NULL
29+
AND pe.is_voided IS FALSE
30+
)
31+
SELECT
32+
*
33+
FROM
34+
duplicate_encounters de
35+
WHERE
36+
de.latest > 1
37+
38+
-----------------------------------------------------------------------------
39+
--Update Query
40+
41+
BEGIN TRANSACTION;
42+
43+
SET ROLE apfodisha;
44+
45+
WITH duplicate_encounters AS (
46+
SELECT
47+
pe.id,
48+
pe.individual_id,
49+
pe.program_enrolment_id,
50+
pe.created_date_time,
51+
ROW_NUMBER() OVER(PARTITION BY pe.individual_id, pe.program_enrolment_id, pe.encounter_type_id ORDER BY pe.created_date_time DESC NULLS LAST) AS latest
52+
FROM
53+
program_encounter pe
54+
JOIN program_enrolment pe2 ON
55+
pe2.id = pe.program_enrolment_id
56+
AND pe.is_voided IS FALSE
57+
JOIN program p ON
58+
p.id = pe2.program_id
59+
AND p.is_voided IS FALSE
60+
AND p.name = 'Child'
61+
JOIN encounter_type et ON
62+
et.id = pe.encounter_type_id
63+
AND et.name = 'Supervisor Child Home visit'
64+
WHERE
65+
pe.encounter_date_time IS NULL
66+
AND pe.cancel_date_time IS NULL
67+
AND pe.is_voided IS FALSE
68+
)
69+
UPDATE
70+
program_encounter pe
71+
SET
72+
is_voided = TRUE,
73+
last_modified_date_time = current_timestamp + (random() * 5000 * (interval '1 millisecond')),
74+
last_modified_by_id = 10917
75+
FROM
76+
duplicate_encounters de
77+
WHERE
78+
pe.id = de.id
79+
AND de.latest > 1;
80+
81+
--COMMIT;
82+
ROLLBACK ;
83+
84+
--------------------------------------------------------------------
85+
86+
87+
88+
89+
90+
91+
92+
-- 1. Removing Duplicate Supervisor Child Home visit
93+
-- Select Query
94+
95+
SET ROLE apfodisha;
96+
97+
WITH duplicate_encounters AS (
98+
SELECT
99+
pe.id,
100+
pe.individual_id,
101+
pe.program_enrolment_id,
102+
pe.created_date_time,
103+
ROW_NUMBER() OVER(PARTITION BY pe.individual_id, pe.program_enrolment_id, pe.encounter_type_id ORDER BY pe.created_date_time DESC NULLS LAST) AS latest
104+
FROM
105+
program_encounter pe
106+
JOIN program_enrolment pe2 ON
107+
pe2.id = pe.program_enrolment_id
108+
AND pe.is_voided IS FALSE
109+
JOIN program p ON
110+
p.id = pe2.program_id
111+
AND p.is_voided IS FALSE
112+
AND p.name = 'Pregnancy'
113+
JOIN encounter_type et ON
114+
et.id = pe.encounter_type_id
115+
AND et.name = 'Supervisor PW Home Visit'
116+
WHERE
117+
pe.encounter_date_time IS NULL
118+
AND pe.cancel_date_time IS NULL
119+
AND pe.is_voided IS FALSE
120+
)
121+
SELECT
122+
*
123+
FROM
124+
duplicate_encounters de
125+
WHERE
126+
de.latest > 1
127+
128+
-----------------------------------------------------------------------------
129+
--Update Query
130+
131+
BEGIN TRANSACTION;
132+
133+
SET ROLE apfodisha;
134+
135+
WITH duplicate_encounters AS (
136+
SELECT
137+
pe.id,
138+
pe.individual_id,
139+
pe.program_enrolment_id,
140+
pe.created_date_time,
141+
ROW_NUMBER() OVER(PARTITION BY pe.individual_id, pe.program_enrolment_id, pe.encounter_type_id ORDER BY pe.created_date_time DESC NULLS LAST) AS latest
142+
FROM
143+
program_encounter pe
144+
JOIN program_enrolment pe2 ON
145+
pe2.id = pe.program_enrolment_id
146+
AND pe.is_voided IS FALSE
147+
JOIN program p ON
148+
p.id = pe2.program_id
149+
AND p.is_voided IS FALSE
150+
AND p.name = 'Pregnancy'
151+
JOIN encounter_type et ON
152+
et.id = pe.encounter_type_id
153+
AND et.name = 'Supervisor PW Home Visit'
154+
WHERE
155+
pe.encounter_date_time IS NULL
156+
AND pe.cancel_date_time IS NULL
157+
AND pe.is_voided IS FALSE
158+
)
159+
UPDATE
160+
program_encounter pe
161+
SET
162+
is_voided = TRUE,
163+
last_modified_date_time = current_timestamp + (random() * 5000 * (interval '1 millisecond')),
164+
last_modified_by_id = 10917
165+
FROM
166+
duplicate_encounters de
167+
WHERE
168+
pe.id = de.id
169+
AND de.latest > 1;
170+
171+
--COMMIT;
172+
ROLLBACK ;
173+
--------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)