-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathstructure.sql
More file actions
9775 lines (7446 loc) · 435 KB
/
structure.sql
File metadata and controls
9775 lines (7446 loc) · 435 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: simple_reporting; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA simple_reporting;
--
-- Name: ltree; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS ltree WITH SCHEMA public;
--
-- Name: EXTENSION ltree; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION ltree IS 'data type for hierarchical tree-like structures';
--
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
--
-- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
--
-- Name: gender_enum; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.gender_enum AS ENUM (
'female',
'male',
'transgender'
);
--
-- Name: add_shard_to_table(date, text); Type: PROCEDURE; Schema: simple_reporting; Owner: -
--
CREATE PROCEDURE simple_reporting.add_shard_to_table(IN target_month_date date, IN table_name text)
LANGUAGE plpgsql
AS $$
DECLARE
monitoring_key TEXT := UPPER(table_name) || '_PARTITION_ALL';
call_internal_statement TEXT ;
BEGIN
call_internal_statement :=
'CALL simple_reporting.generate_and_attach_shard_to_table(TO_DATE('''
|| TO_CHAR(target_month_date, 'YYYY-MM')
|| ''', ''YYYY-MM''),'''|| table_name ||''');';
CALL simple_reporting.monitored_execute(
gen_random_uuid(),
monitoring_key,
target_month_date,
call_internal_statement
);
END;
$$;
--
-- Name: generate_and_attach_shard_to_table(date, text); Type: PROCEDURE; Schema: simple_reporting; Owner: -
--
CREATE PROCEDURE simple_reporting.generate_and_attach_shard_to_table(IN start_date date, IN table_name text)
LANGUAGE plpgsql
AS $$
DECLARE
target_reference_date DATE := date_trunc('month', start_date)::DATE;
target_table_key TEXT := TO_CHAR(target_reference_date, 'YYYYMMDD');
target_to_date TEXT := 'date_trunc(''month'', TO_DATE(''' || target_table_key || ''', ''YYYYMMDD''))::date';
target_table_name TEXT := 'simple_reporting.'|| table_name || '_' || target_table_key;
partition_drop_monitoring_key TEXT := UPPER(table_name) || '_PARTITION_DROP';
ctas_monitoring_key TEXT := UPPER(table_name) || '_PARTITION_CTAS';
partition_check_monitoring_key TEXT := UPPER(table_name) || '_PARTITION_CHECK';
partition_attach_monitoring_key TEXT := UPPER(table_name) || '_PARTITION_SHARD';
drop_statement TEXT := 'DROP TABLE IF EXISTS ' || target_table_name || ';';
ctas_statement TEXT :=
'CREATE TABLE ' || target_table_name ||
' AS SELECT * FROM simple_reporting.' || table_name || '_table_function(' ||
target_to_date || ');';
check_statement TEXT :=
'ALTER TABLE ' || target_table_name ||
' ADD CONSTRAINT ' || table_name || '_month_date_shard_check CHECK (month_date = ' ||
target_to_date || ');';
shard_statement TEXT :=
'ALTER TABLE simple_reporting.' || table_name || ' ATTACH PARTITION ' ||
target_table_name || ' FOR VALUES IN (' || target_to_date || ');';
run_key UUID := gen_random_uuid();
BEGIN
CALL simple_reporting.monitored_execute(run_key, partition_drop_monitoring_key, target_reference_date, drop_statement);
CALL simple_reporting.monitored_execute(run_key, ctas_monitoring_key, target_reference_date, ctas_statement);
CALL simple_reporting.monitored_execute(run_key, partition_check_monitoring_key, target_reference_date, check_statement);
CALL simple_reporting.monitored_execute(run_key, partition_attach_monitoring_key, target_reference_date, shard_statement);
END;
$$;
--
-- Name: monitored_execute(uuid, text, date, text); Type: PROCEDURE; Schema: simple_reporting; Owner: -
--
CREATE PROCEDURE simple_reporting.monitored_execute(IN run_id uuid, IN action_key text, IN target_month_date date, IN target_query text)
LANGUAGE plpgsql
AS $$
DECLARE
internal_start_date TIMESTAMP := clock_timestamp();
BEGIN
RAISE NOTICE 'EXECUTING <%>: %', action_key, target_query;
BEGIN
EXECUTE target_query;
INSERT INTO simple_reporting.simple_reporting_runs (run_key, action_name, target_date, start_date, end_date, action_status)
VALUES (run_id, action_key, target_month_date, internal_start_date, clock_timestamp(), 'OK');
EXCEPTION WHEN OTHERS THEN
INSERT INTO simple_reporting.simple_reporting_runs (run_key, action_name, target_date, start_date, end_date, action_status, sql_state, sql_error_message)
VALUES (run_id, action_key, target_month_date, internal_start_date, clock_timestamp(), 'ERROR', SQLSTATE, SQLERRM);
END;
END;
$$;
SET default_tablespace = '';
--
-- Name: reporting_patient_states; Type: TABLE; Schema: simple_reporting; Owner: -
--
CREATE TABLE simple_reporting.reporting_patient_states (
patient_id uuid,
recorded_at timestamp without time zone,
status character varying,
gender character varying,
age integer,
age_updated_at timestamp without time zone,
date_of_birth date,
current_age double precision,
month_date date,
month double precision,
quarter double precision,
year double precision,
month_string text,
quarter_string text,
hypertension text,
prior_heart_attack text,
prior_stroke text,
chronic_kidney_disease text,
receiving_treatment_for_hypertension text,
diabetes text,
assigned_facility_id uuid,
assigned_facility_size character varying,
assigned_facility_type character varying,
assigned_facility_slug character varying,
assigned_facility_region_id uuid,
assigned_block_slug character varying,
assigned_block_region_id uuid,
assigned_district_slug character varying,
assigned_district_region_id uuid,
assigned_state_slug character varying,
assigned_state_region_id uuid,
assigned_organization_slug character varying,
assigned_organization_region_id uuid,
registration_facility_id uuid,
registration_facility_size character varying,
registration_facility_type character varying,
registration_facility_slug character varying,
registration_facility_region_id uuid,
registration_block_slug character varying,
registration_block_region_id uuid,
registration_district_slug character varying,
registration_district_region_id uuid,
registration_state_slug character varying,
registration_state_region_id uuid,
registration_organization_slug character varying,
registration_organization_region_id uuid,
blood_pressure_id uuid,
bp_facility_id uuid,
bp_recorded_at timestamp without time zone,
systolic integer,
diastolic integer,
blood_sugar_id uuid,
bs_facility_id uuid,
bs_recorded_at timestamp without time zone,
blood_sugar_type character varying,
blood_sugar_value numeric,
blood_sugar_risk_state text,
encounter_id uuid,
encounter_recorded_at timestamp without time zone,
prescription_drug_id uuid,
prescription_drug_recorded_at timestamp without time zone,
appointment_id uuid,
appointment_recorded_at timestamp without time zone,
visited_facility_ids uuid[],
months_since_registration double precision,
quarters_since_registration double precision,
months_since_visit double precision,
quarters_since_visit double precision,
months_since_bp double precision,
quarters_since_bp double precision,
months_since_bs double precision,
quarters_since_bs double precision,
last_bp_state text,
htn_care_state text,
htn_treatment_outcome_in_last_3_months text,
htn_treatment_outcome_in_last_2_months text,
htn_treatment_outcome_in_quarter text,
diabetes_treatment_outcome_in_last_3_months text,
diabetes_treatment_outcome_in_last_2_months text,
diabetes_treatment_outcome_in_quarter text,
titrated boolean,
diagnosed_confirmed_at timestamp without time zone
)
PARTITION BY LIST (month_date);
--
-- Name: reporting_patient_states_table_function(date); Type: FUNCTION; Schema: simple_reporting; Owner: -
--
CREATE OR REPLACE FUNCTION simple_reporting.reporting_patient_states_table_function(date) RETURNS SETOF simple_reporting.reporting_patient_states
LANGUAGE plpgsql
AS $_$
BEGIN
RETURN QUERY
SELECT DISTINCT ON (p.id)
-- Basic patient identifiers
p.id AS patient_id,
p.recorded_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' AS recorded_at,
p.status,
p.gender,
p.age,
p.age_updated_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' AS age_updated_at,
p.date_of_birth,
EXTRACT(YEAR FROM COALESCE(
age(p.date_of_birth),
make_interval(years => p.age) + age(p.age_updated_at)
))::float8 AS current_age,
-- Calendar
cal.month_date,
cal.month,
cal.quarter,
cal.year,
cal.month_string,
cal.quarter_string,
-- Medical history
mh.hypertension,
mh.prior_heart_attack,
mh.prior_stroke,
mh.chronic_kidney_disease,
mh.receiving_treatment_for_hypertension,
mh.diabetes,
-- Assigned facility and regions
p.assigned_facility_id,
assigned_facility.facility_size,
assigned_facility.facility_type,
assigned_facility.facility_region_slug,
assigned_facility.facility_region_id,
assigned_facility.block_slug,
assigned_facility.block_region_id,
assigned_facility.district_slug,
assigned_facility.district_region_id,
assigned_facility.state_slug,
assigned_facility.state_region_id,
assigned_facility.organization_slug,
assigned_facility.organization_region_id,
-- Registration facility and regions
p.registration_facility_id,
registration_facility.facility_size,
registration_facility.facility_type,
registration_facility.facility_region_slug,
registration_facility.facility_region_id,
registration_facility.block_slug,
registration_facility.block_region_id,
registration_facility.district_slug,
registration_facility.district_region_id,
registration_facility.state_slug,
registration_facility.state_region_id,
registration_facility.organization_slug,
registration_facility.organization_region_id,
-- Visit details
bps.blood_pressure_id,
bps.blood_pressure_facility_id AS bp_facility_id,
bps.blood_pressure_recorded_at AS bp_recorded_at,
bps.systolic,
bps.diastolic,
bss.blood_sugar_id,
bss.blood_sugar_facility_id AS bs_facility_id,
bss.blood_sugar_recorded_at AS bs_recorded_at,
bss.blood_sugar_type,
bss.blood_sugar_value,
bss.blood_sugar_risk_state,
visits.encounter_id,
visits.encounter_recorded_at,
visits.prescription_drug_id,
visits.prescription_drug_recorded_at,
visits.appointment_id,
visits.appointment_recorded_at,
visits.visited_facility_ids,
-- Relative time calculations
(cal.year - DATE_PART('year', p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) * 12 +
(cal.month - DATE_PART('month', p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE'))))
AS months_since_registration,
(cal.year - DATE_PART('year', p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) * 4 +
(cal.quarter - DATE_PART('quarter', p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE'))))
AS quarters_since_registration,
visits.months_since_visit,
visits.quarters_since_visit,
bps.months_since_bp,
bps.quarters_since_bp,
bss.months_since_bs,
bss.quarters_since_bs,
-- BP and treatment indicators
CASE
WHEN bps.systolic IS NULL OR bps.diastolic IS NULL THEN 'unknown'
WHEN bps.systolic < 140 AND bps.diastolic < 90 THEN 'controlled'
ELSE 'uncontrolled'
END AS last_bp_state,
CASE
WHEN p.status = 'dead' THEN 'dead'
WHEN (
(cal.year - DATE_PART('year', p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) * 12 +
(cal.month - DATE_PART('month', p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE (SELECT current_setting('TIMEZONE')))) < 12
OR visits.months_since_visit < 12
) THEN 'under_care'
ELSE 'lost_to_follow_up'
END AS htn_care_state,
CASE
WHEN visits.months_since_visit >= 3 OR visits.months_since_visit IS NULL THEN 'missed_visit'
WHEN bps.months_since_bp >= 3 OR bps.months_since_bp IS NULL THEN 'visited_no_bp'
WHEN bps.systolic < 140 AND bps.diastolic < 90 THEN 'controlled'
ELSE 'uncontrolled'
END AS htn_treatment_outcome_in_last_3_months,
CASE
WHEN visits.months_since_visit >= 2 OR visits.months_since_visit IS NULL THEN 'missed_visit'
WHEN bps.months_since_bp >= 2 OR bps.months_since_bp IS NULL THEN 'visited_no_bp'
WHEN bps.systolic < 140 AND bps.diastolic < 90 THEN 'controlled'
ELSE 'uncontrolled'
END AS htn_treatment_outcome_in_last_2_months,
CASE
WHEN visits.quarters_since_visit >= 1 OR visits.quarters_since_visit IS NULL THEN 'missed_visit'
WHEN bps.quarters_since_bp >= 1 OR bps.quarters_since_bp IS NULL THEN 'visited_no_bp'
WHEN bps.systolic < 140 AND bps.diastolic < 90 THEN 'controlled'
ELSE 'uncontrolled'
END AS htn_treatment_outcome_in_quarter,
CASE
WHEN (visits.months_since_visit >= 3 OR visits.months_since_visit is NULL) THEN 'missed_visit'
WHEN (bss.months_since_bs >= 3 OR bss.months_since_bs is NULL) THEN 'visited_no_bs'
ELSE bss.blood_sugar_risk_state
END AS diabetes_treatment_outcome_in_last_3_months,
CASE
WHEN visits.months_since_visit >= 2 OR visits.months_since_visit IS NULL THEN 'missed_visit'
WHEN bss.months_since_bs >= 2 OR bss.months_since_bs IS NULL THEN 'visited_no_bs'
ELSE bss.blood_sugar_risk_state
END AS diabetes_treatment_outcome_in_last_2_months,
CASE
WHEN visits.quarters_since_visit >= 1 OR visits.quarters_since_visit IS NULL THEN 'missed_visit'
WHEN bss.quarters_since_bs >= 1 OR bss.quarters_since_bs IS NULL THEN 'visited_no_bs'
ELSE bss.blood_sugar_risk_state
END AS diabetes_treatment_outcome_in_quarter,
(
current_meds.amlodipine > past_meds.amlodipine OR
current_meds.telmisartan > past_meds.telmisartan OR
current_meds.losartan > past_meds.losartan OR
current_meds.atenolol > past_meds.atenolol OR
current_meds.enalapril > past_meds.enalapril OR
current_meds.chlorthalidone > past_meds.chlorthalidone OR
current_meds.hydrochlorothiazide > past_meds.hydrochlorothiazide
) AS titrated,
p.diagnosed_confirmed_at AT TIME ZONE 'UTC' AT TIME ZONE 'UTC' AS diagnosed_confirmed_at
FROM public.patients p
JOIN public.reporting_months cal
ON cal.month_date = $1
AND p.diagnosed_confirmed_at <= cal.month_date + INTERVAL '1 month' + INTERVAL '1 day'
AND ((
to_char(timezone((SELECT current_setting('TIMEZONE'::text) AS current_setting), TIMEZONE('UTC'::text, p.diagnosed_confirmed_at)), 'YYYY-MM'::text) <=
to_char((cal.month_date)::timestamp with time zone, 'YYYY-MM'::text))
)
LEFT OUTER JOIN public.reporting_patient_blood_pressures bps
ON p.id = bps.patient_id AND cal.month_date = bps.month_date
LEFT OUTER JOIN public.reporting_patient_blood_sugars bss
ON p.id = bss.patient_id AND cal.month_date = bss.month_date
LEFT OUTER JOIN public.reporting_patient_visits visits
ON p.id = visits.patient_id AND cal.month_date = visits.month_date
LEFT OUTER JOIN LATERAL (
SELECT DISTINCT ON (patient_id) *
FROM public.medical_histories
WHERE patient_id = p.id AND deleted_at IS NULL
ORDER BY patient_id, device_updated_at DESC
) mh ON true
LEFT OUTER JOIN public.reporting_prescriptions current_meds
ON current_meds.patient_id = p.id AND cal.month_date = current_meds.month_date
LEFT OUTER JOIN public.reporting_prescriptions past_meds
ON past_meds.patient_id = p.id AND cal.month_date = past_meds.month_date + INTERVAL '1 month'
INNER JOIN public.reporting_facilities registration_facility
ON registration_facility.facility_id = p.registration_facility_id
INNER JOIN public.reporting_facilities assigned_facility
ON assigned_facility.facility_id = p.assigned_facility_id
WHERE p.deleted_at IS NULL
AND p.diagnosed_confirmed_at IS NOT NULL
ORDER BY p.id;
END;
$_$;
--
-- Name: reporting_facility_monthly_follow_ups_and_registrations; Type: TABLE; Schema: simple_reporting; Owner: -
--
CREATE TABLE IF NOT EXISTS simple_reporting.reporting_facility_monthly_follow_ups_and_registrations (
facility_region_slug character varying,
facility_id uuid,
facility_region_id uuid,
block_region_id uuid,
district_region_id uuid,
state_region_id uuid,
month_date date,
monthly_registrations_all bigint,
monthly_registrations_htn_all bigint,
monthly_registrations_htn_male bigint,
monthly_registrations_htn_female bigint,
monthly_registrations_htn_transgender bigint,
monthly_registrations_dm_all bigint,
monthly_registrations_dm_male bigint,
monthly_registrations_dm_female bigint,
monthly_registrations_dm_transgender bigint,
monthly_follow_ups_all bigint,
monthly_follow_ups_htn_all bigint,
monthly_follow_ups_htn_female bigint,
monthly_follow_ups_htn_male bigint,
monthly_follow_ups_htn_transgender bigint,
monthly_follow_ups_dm_all bigint,
monthly_follow_ups_dm_female bigint,
monthly_follow_ups_dm_male bigint,
monthly_follow_ups_dm_transgender bigint,
monthly_registrations_htn_or_dm bigint,
monthly_registrations_htn_only bigint,
monthly_registrations_htn_only_male bigint,
monthly_registrations_htn_only_female bigint,
monthly_registrations_htn_only_transgender bigint,
monthly_registrations_dm_only bigint,
monthly_registrations_dm_only_male bigint,
monthly_registrations_dm_only_female bigint,
monthly_registrations_dm_only_transgender bigint,
monthly_registrations_htn_and_dm bigint,
monthly_registrations_htn_and_dm_male bigint,
monthly_registrations_htn_and_dm_female bigint,
monthly_registrations_htn_and_dm_transgender bigint,
monthly_follow_ups_htn_or_dm bigint,
monthly_follow_ups_htn_only bigint,
monthly_follow_ups_htn_only_female bigint,
monthly_follow_ups_htn_only_male bigint,
monthly_follow_ups_htn_only_transgender bigint,
monthly_follow_ups_dm_only bigint,
monthly_follow_ups_dm_only_female bigint,
monthly_follow_ups_dm_only_male bigint,
monthly_follow_ups_dm_only_transgender bigint,
monthly_follow_ups_htn_and_dm bigint,
monthly_follow_ups_htn_and_dm_male bigint,
monthly_follow_ups_htn_and_dm_female bigint,
monthly_follow_ups_htn_and_dm_transgender bigint
)
PARTITION BY LIST (month_date);
--
-- Name: reporting_facility_monthly_follow_ups_and_registrations_table_function(date); Type: FUNCTION; Schema: simple_reporting; Owner: -
--
CREATE OR REPLACE FUNCTION simple_reporting.reporting_facility_monthly_follow_ups_and_registrations_table_function(date) RETURNS SETOF simple_reporting.reporting_facility_monthly_follow_ups_and_registrations
LANGUAGE plpgsql
AS $_$
BEGIN
RETURN QUERY
WITH monthly_registration_patient_states AS (
SELECT
registration_facility_id AS facility_id,
month_date,
gender,
hypertension,
diabetes
FROM reporting_patient_states
WHERE months_since_registration = 0
AND month_date = $1
), registered_patients AS (
SELECT
facility_id,
month_date,
count(*) AS monthly_registrations_all,
count(*) FILTER (WHERE hypertension = 'yes') AS monthly_registrations_htn_all,
count(*) FILTER (WHERE hypertension = 'yes' and gender = 'female') AS monthly_registrations_htn_female,
count(*) FILTER (WHERE hypertension = 'yes' and gender = 'male') AS monthly_registrations_htn_male,
count(*) FILTER (WHERE hypertension = 'yes' and gender = 'transgender') AS monthly_registrations_htn_transgender,
count(*) FILTER (WHERE diabetes = 'yes') AS monthly_registrations_dm_all,
count(*) FILTER (WHERE diabetes = 'yes' and gender = 'female') AS monthly_registrations_dm_female,
count(*) FILTER (WHERE diabetes = 'yes' and gender = 'male') AS monthly_registrations_dm_male,
count(*) FILTER (WHERE diabetes = 'yes' and gender = 'transgender') AS monthly_registrations_dm_transgender,
count(*) FILTER (WHERE hypertension = 'yes' or diabetes = 'yes') AS monthly_registrations_htn_or_dm,
count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_registrations_htn_and_dm,
count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'female') AS monthly_registrations_htn_and_dm_female,
count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'male') AS monthly_registrations_htn_and_dm_male,
count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'transgender') AS monthly_registrations_htn_and_dm_transgender,
count(*) FILTER (WHERE hypertension = 'yes')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_registrations_htn_only,
count(*) FILTER (WHERE hypertension = 'yes' and gender = 'female')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'female') AS monthly_registrations_htn_only_female,
count(*) FILTER (WHERE hypertension = 'yes' and gender = 'male')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'male') AS monthly_registrations_htn_only_male,
count(*) FILTER (WHERE hypertension = 'yes' and gender = 'transgender')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'transgender')AS monthly_registrations_htn_only_transgender,
count(*) FILTER (WHERE diabetes = 'yes')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_registrations_dm_only,
count(*) FILTER (WHERE diabetes = 'yes' and gender = 'female')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'female') AS monthly_registrations_dm_only_female,
count(*) FILTER (WHERE diabetes = 'yes' and gender = 'male')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'male') AS monthly_registrations_dm_only_male,
count(*) FILTER (WHERE diabetes = 'yes' and gender = 'transgender')
- count(*) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and gender = 'transgender')AS monthly_registrations_dm_only_transgender
FROM monthly_registration_patient_states
GROUP BY facility_id, month_date
), follow_ups AS (
SELECT
facility_id,
month_date,
count(distinct(patient_id)) AS monthly_follow_ups_all,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes') AS monthly_follow_ups_htn_all,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'female') AS monthly_follow_ups_htn_female,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'male') AS monthly_follow_ups_htn_male,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'transgender') AS monthly_follow_ups_htn_transgender,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes') AS monthly_follow_ups_dm_all,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_dm_female,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_dm_male,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'transgender') AS monthly_follow_ups_dm_transgender,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' or diabetes = 'yes') AS monthly_follow_ups_htn_or_dm,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_follow_ups_htn_and_dm,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_htn_and_dm_female,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_htn_and_dm_male,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'transgender') AS monthly_follow_ups_htn_and_dm_transgender,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_follow_ups_htn_only,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'female')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_htn_only_female,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'male')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_htn_only_male,
count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and patient_gender = 'transgender')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'transgender')AS monthly_follow_ups_htn_only_transgender,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes') AS monthly_follow_ups_dm_only,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'female')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'female') AS monthly_follow_ups_dm_only_female,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'male')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'male') AS monthly_follow_ups_dm_only_male,
count(distinct(patient_id)) FILTER (WHERE diabetes = 'yes' and patient_gender = 'transgender')
- count(distinct(patient_id)) FILTER (WHERE hypertension = 'yes' and diabetes = 'yes' and patient_gender = 'transgender')AS monthly_follow_ups_dm_only_transgender
FROM reporting_patient_follow_ups
WHERE month_date = $1
GROUP BY facility_id, month_date
)
SELECT
rf.facility_region_slug,
rf.facility_id,
rf.facility_region_id,
rf.block_region_id,
rf.district_region_id,
rf.state_region_id,
cal.month_date,
coalesce(registered_patients.monthly_registrations_all,0) AS monthly_registrations_all,
coalesce(registered_patients.monthly_registrations_htn_all,0) AS monthly_registrations_htn_all,
coalesce(registered_patients.monthly_registrations_htn_male,0) AS monthly_registrations_htn_male,
coalesce(registered_patients.monthly_registrations_htn_female,0) AS monthly_registrations_htn_female,
coalesce(registered_patients.monthly_registrations_htn_transgender,0) AS monthly_registrations_htn_transgender,
coalesce(registered_patients.monthly_registrations_dm_all,0) AS monthly_registrations_dm_all,
coalesce(registered_patients.monthly_registrations_dm_male,0) AS monthly_registrations_dm_male,
coalesce(registered_patients.monthly_registrations_dm_female,0) AS monthly_registrations_dm_female,
coalesce(registered_patients.monthly_registrations_dm_transgender,0) AS monthly_registrations_dm_transgender,
coalesce(follow_ups.monthly_follow_ups_all,0) AS monthly_follow_ups_all,
coalesce(follow_ups.monthly_follow_ups_htn_all,0) AS monthly_follow_ups_htn_all,
coalesce(follow_ups.monthly_follow_ups_htn_female,0) AS monthly_follow_ups_htn_female,
coalesce(follow_ups.monthly_follow_ups_htn_male,0) AS monthly_follow_ups_htn_male,
coalesce(follow_ups.monthly_follow_ups_htn_transgender,0) AS monthly_follow_ups_htn_transgender,
coalesce(follow_ups.monthly_follow_ups_dm_all,0) AS monthly_follow_ups_dm_all,
coalesce(follow_ups.monthly_follow_ups_dm_female,0) AS monthly_follow_ups_dm_female,
coalesce(follow_ups.monthly_follow_ups_dm_male,0) AS monthly_follow_ups_dm_male,
coalesce(follow_ups.monthly_follow_ups_dm_transgender,0) AS monthly_follow_ups_dm_transgender,
coalesce(registered_patients.monthly_registrations_htn_or_dm, 0) AS monthly_registrations_htn_or_dm,
coalesce(registered_patients.monthly_registrations_htn_only, 0) AS monthly_registrations_htn_only,
coalesce(registered_patients.monthly_registrations_htn_only_male, 0) AS monthly_registrations_htn_only_male,
coalesce(registered_patients.monthly_registrations_htn_only_female, 0) AS monthly_registrations_htn_only_female,
coalesce(registered_patients.monthly_registrations_htn_only_transgender, 0) AS monthly_registrations_htn_only_transgender,
coalesce(registered_patients.monthly_registrations_dm_only, 0) AS monthly_registrations_dm_only,
coalesce(registered_patients.monthly_registrations_dm_only_male, 0) AS monthly_registrations_dm_only_male,
coalesce(registered_patients.monthly_registrations_dm_only_female, 0) AS monthly_registrations_dm_only_female,
coalesce(registered_patients.monthly_registrations_dm_only_transgender, 0) AS monthly_registrations_dm_only_transgender,
coalesce(registered_patients.monthly_registrations_htn_and_dm, 0) AS monthly_registrations_htn_and_dm,
coalesce(registered_patients.monthly_registrations_htn_and_dm_male, 0) AS monthly_registrations_htn_and_dm_male,
coalesce(registered_patients.monthly_registrations_htn_and_dm_female, 0) AS monthly_registrations_htn_and_dm_female,
coalesce(registered_patients.monthly_registrations_htn_and_dm_transgender, 0) AS monthly_registrations_htn_and_dm_transgender,
coalesce(follow_ups.monthly_follow_ups_htn_or_dm, 0) AS monthly_follow_ups_htn_or_dm,
coalesce(follow_ups.monthly_follow_ups_htn_only, 0) AS monthly_follow_ups_htn_only,
coalesce(follow_ups.monthly_follow_ups_htn_only_female, 0) AS monthly_follow_ups_htn_only_female,
coalesce(follow_ups.monthly_follow_ups_htn_only_male, 0) AS monthly_follow_ups_htn_only_male,
coalesce(follow_ups.monthly_follow_ups_htn_only_transgender, 0) AS monthly_follow_ups_htn_only_transgender,
coalesce(follow_ups.monthly_follow_ups_dm_only, 0) AS monthly_follow_ups_dm_only,
coalesce(follow_ups.monthly_follow_ups_dm_only_female, 0) AS monthly_follow_ups_dm_only_female,
coalesce(follow_ups.monthly_follow_ups_dm_only_male, 0) AS monthly_follow_ups_dm_only_male,
coalesce(follow_ups.monthly_follow_ups_dm_only_transgender, 0) AS monthly_follow_ups_dm_only_transgender,
coalesce(follow_ups.monthly_follow_ups_htn_and_dm, 0) AS monthly_follow_ups_htn_and_dm,
coalesce(follow_ups.monthly_follow_ups_htn_and_dm_male, 0) AS monthly_follow_ups_htn_and_dm_male,
coalesce(follow_ups.monthly_follow_ups_htn_and_dm_female, 0) AS monthly_follow_ups_htn_and_dm_female,
coalesce(follow_ups.monthly_follow_ups_htn_and_dm_transgender, 0) AS monthly_follow_ups_htn_and_dm_transgender
FROM reporting_facilities rf
INNER JOIN reporting_months cal
ON cal.month_date = $1
LEFT OUTER JOIN registered_patients
ON registered_patients.month_date = cal.month_date
AND registered_patients.facility_id = rf.facility_id
LEFT OUTER JOIN follow_ups
ON follow_ups.month_date = cal.month_date
AND follow_ups.facility_id = rf.facility_id
ORDER BY cal.month_date desc;
END;
$_$;
--
-- Name: reporting_patient_prescriptions; Type: TABLE; Schema: simple_reporting; Owner: -
--
CREATE TABLE IF NOT EXISTS simple_reporting.reporting_patient_prescriptions (
patient_id uuid,
month_date date,
age integer,
gender character varying,
hypertension text,
diabetes text,
last_bp_state text,
htn_care_state text,
months_since_bp double precision,
assigned_facility_id uuid,
assigned_facility_name character varying,
assigned_facility_size character varying,
assigned_facility_type character varying,
assigned_facility_slug character varying,
assigned_facility_region_id uuid,
assigned_block_slug character varying,
assigned_block_region_id uuid,
assigned_block_name character varying,
assigned_district_slug character varying,
assigned_district_region_id uuid,
assigned_district_name character varying,
assigned_state_slug character varying,
assigned_state_region_id uuid,
assigned_state_name character varying,
assigned_organization_slug character varying,
assigned_organization_region_id uuid,
assigned_organization_name character varying,
registration_facility_id uuid,
registration_facility_name character varying,
registration_facility_size character varying,
registration_facility_type character varying,
registration_facility_slug character varying,
registration_facility_region_id uuid,
registration_block_slug character varying,
registration_block_region_id uuid,
registration_block_name character varying,
registration_district_slug character varying,
registration_district_region_id uuid,
registration_district_name character varying,
registration_state_slug character varying,
registration_state_region_id uuid,
registration_state_name character varying,
registration_organization_slug character varying,
registration_organization_region_id uuid,
registration_organization_name character varying,
hypertension_prescriptions jsonb,
diabetes_prescriptions jsonb,
other_prescriptions jsonb,
previous_hypertension_prescriptions jsonb,
previous_diabetes_prescriptions jsonb,
previous_other_prescriptions jsonb,
hypertension_drug_changed boolean,
diabetes_drug_changed boolean,
other_drug_changed boolean,
prescribed_statins boolean,
latest_cvd_risk_score_lower_range integer,
latest_cvd_risk_score_upper_range integer
)
PARTITION BY LIST (month_date);
--
-- Name: normalize_jsonb_array(input jsonb); Type: FUNCTION; Schema: simple_reporting; Owner: -
--
CREATE OR REPLACE FUNCTION simple_reporting.normalize_jsonb_array(input jsonb)
RETURNS jsonb
LANGUAGE sql
IMMUTABLE
AS $$
SELECT
CASE
WHEN input IS NULL THEN NULL
ELSE (
SELECT jsonb_agg(elem ORDER BY elem)
FROM jsonb_array_elements(input) elem
)
END;
$$;
--
-- Name: reporting_patient_prescriptions_table_function(date); Type: FUNCTION; Schema: simple_reporting; Owner: -
--
CREATE OR REPLACE FUNCTION simple_reporting.reporting_patient_prescriptions_table_function(date) RETURNS SETOF simple_reporting.reporting_patient_prescriptions
LANGUAGE plpgsql
AS $_$
BEGIN
RETURN QUERY
SELECT
rps.patient_id,
rps.month_date,
rps.age,
rps.gender,
rps.hypertension,
rps.diabetes,
rps.last_bp_state,
rps.htn_care_state,
rps.months_since_bp,
rps.assigned_facility_id,
assigned_facility.facility_name AS assigned_facility_name,
rps.assigned_facility_size,
rps.assigned_facility_type,
rps.assigned_facility_slug,
rps.assigned_facility_region_id,
rps.assigned_block_slug,
rps.assigned_block_region_id,
assigned_facility.block_name AS assigned_block_name,
rps.assigned_district_slug,
rps.assigned_district_region_id,
assigned_facility.district_name AS assigned_district_name,
rps.assigned_state_slug,
rps.assigned_state_region_id,
assigned_facility.state_name AS assigned_state_name,
rps.assigned_organization_slug,
rps.assigned_organization_region_id,
assigned_facility.organization_name AS assigned_organization_name,
rps.registration_facility_id,
registration_facility.facility_name AS registration_facility_name,
rps.registration_facility_size,
rps.registration_facility_type,
rps.registration_facility_slug,
rps.registration_facility_region_id,
rps.registration_block_slug,
rps.registration_block_region_id,
registration_facility.block_name AS registration_block_name,
rps.registration_district_slug,
rps.registration_district_region_id,
registration_facility.district_name AS registration_district_name,
rps.registration_state_slug,
rps.registration_state_region_id,
registration_facility.state_name AS registration_state_name,
rps.registration_organization_slug,
rps.registration_organization_region_id,
registration_facility.organization_name AS registration_organization_name,
curr.hypertension_prescriptions,
curr.diabetes_prescriptions,
curr.other_prescriptions,
prev.hypertension_prescriptions AS previous_hypertension_prescriptions,
prev.diabetes_prescriptions AS previous_diabetes_prescriptions,
prev.other_prescriptions AS previous_other_prescriptions,
(
simple_reporting.normalize_jsonb_array(curr.hypertension_prescriptions)
IS DISTINCT FROM
simple_reporting.normalize_jsonb_array(prev.hypertension_prescriptions)
) AS hypertension_drug_changed,
(
simple_reporting.normalize_jsonb_array(curr.diabetes_prescriptions)
IS DISTINCT FROM
simple_reporting.normalize_jsonb_array(prev.diabetes_prescriptions)
) AS diabetes_drug_changed,
(
simple_reporting.normalize_jsonb_array(curr.other_prescriptions)
IS DISTINCT FROM
simple_reporting.normalize_jsonb_array(prev.other_prescriptions)
) AS other_drug_changed,
(
EXISTS (
SELECT 1
FROM jsonb_array_elements(
COALESCE(curr.hypertension_prescriptions, '[]'::jsonb)
|| COALESCE(curr.diabetes_prescriptions, '[]'::jsonb)
|| COALESCE(curr.other_prescriptions, '[]'::jsonb)
) elem
WHERE elem->>'drug_name' ILIKE '%statin%'
)
) AS prescribed_statins,
cvd.latest_cvd_risk_score_lower_range,
cvd.latest_cvd_risk_score_upper_range
FROM simple_reporting.reporting_patient_states rps
LEFT JOIN reporting_facilities assigned_facility ON rps.assigned_facility_id = assigned_facility.facility_id
LEFT JOIN reporting_facilities registration_facility ON rps.registration_facility_id = registration_facility.facility_id
LEFT JOIN LATERAL (
SELECT
jsonb_agg(jsonb_build_object(
'drug_name', pd.name,
'dosage', pd.dosage
)) FILTER (WHERE mp.hypertension = true) AS hypertension_prescriptions,
jsonb_agg(jsonb_build_object(
'drug_name', pd.name,
'dosage', pd.dosage
)) FILTER (WHERE mp.diabetes = true) AS diabetes_prescriptions,
jsonb_agg(jsonb_build_object(
'drug_name', pd.name,
'dosage', pd.dosage
)) FILTER (
WHERE
mp.name IS NULL OR (
COALESCE(mp.hypertension,false) = false
AND COALESCE(mp.diabetes,false) = false
)
) AS other_prescriptions
FROM prescription_drugs pd
LEFT JOIN medicine_purposes mp ON mp.name = pd.name
WHERE pd.patient_id = rps.patient_id
AND pd.deleted_at IS NULL
AND to_char(timezone(current_setting('TIMEZONE'), timezone('UTC', pd.device_created_at)),'YYYY-MM') <= to_char(rps.month_date, 'YYYY-MM')
AND (pd.is_deleted = false
OR (
pd.is_deleted = true
AND to_char(timezone(current_setting('TIMEZONE'), timezone('UTC', pd.device_updated_at)), 'YYYY-MM') > to_char(rps.month_date, 'YYYY-MM')
)
)
) curr ON TRUE
LEFT JOIN LATERAL (
SELECT
jsonb_agg(jsonb_build_object(
'drug_name', pd.name,
'dosage', pd.dosage
)) FILTER (WHERE mp.hypertension = true) AS hypertension_prescriptions,
jsonb_agg(jsonb_build_object(
'drug_name', pd.name,
'dosage', pd.dosage
)) FILTER (WHERE mp.diabetes = true) AS diabetes_prescriptions,
jsonb_agg(jsonb_build_object(
'drug_name', pd.name,
'dosage', pd.dosage
)) FILTER (
WHERE
mp.name IS NULL OR (
COALESCE(mp.hypertension,false) = false
AND COALESCE(mp.diabetes,false) = false
)
) AS other_prescriptions
FROM prescription_drugs pd
LEFT JOIN medicine_purposes mp ON mp.name = pd.name
WHERE pd.patient_id = rps.patient_id
AND pd.deleted_at IS NULL
AND to_char(timezone(current_setting('TIMEZONE'), timezone('UTC', pd.device_created_at)), 'YYYY-MM') <= to_char(rps.month_date - INTERVAL '1 month', 'YYYY-MM')
AND ( pd.is_deleted = false
OR (
pd.is_deleted = true
AND to_char(timezone(current_setting('TIMEZONE'), timezone('UTC', pd.device_updated_at)), 'YYYY-MM') > to_char(rps.month_date - INTERVAL '1 month', 'YYYY-MM')
)
)
) prev ON TRUE
LEFT JOIN LATERAL (
SELECT
split_part(cr.risk_score,'-',1)::int AS latest_cvd_risk_score_lower_range,
COALESCE(
NULLIF(split_part(cr.risk_score,'-',2),''),
split_part(cr.risk_score,'-',1)
)::int AS latest_cvd_risk_score_upper_range
FROM cvd_risks cr
WHERE cr.patient_id = rps.patient_id
AND cr.deleted_at IS NULL
AND date_trunc('month',
timezone(current_setting('TIMEZONE'),
timezone('UTC', cr.device_updated_at))
) < (rps.month_date + interval '1 month')
ORDER BY cr.device_updated_at DESC
LIMIT 1
) cvd ON TRUE
WHERE rps.month_date = $1
AND rps.htn_care_state <> 'dead';
END;
$_$;
SET default_table_access_method = heap;
--
-- Name: reporting_patient_prescriptions; Type: VIEW; Schema: public; Owner: -
--
CREATE OR REPLACE VIEW public.reporting_patient_prescriptions AS SELECT * FROM simple_reporting.reporting_patient_prescriptions;
--
-- Name: reporting_facility_states; Type: TABLE; Schema: simple_reporting; Owner: -
--
CREATE TABLE IF NOT EXISTS simple_reporting.reporting_facility_states (
month_date date,
month double precision,
quarter double precision,
year double precision,
month_string text,
quarter_string text,
facility_id uuid,
facility_name character varying,
facility_type character varying,
facility_size character varying,
facility_region_id uuid,
facility_region_name character varying,
facility_region_slug character varying,
block_region_id uuid,