# Generated by Django 5.2.6 on 2025-09-25 23:42

import django_db_views.migration_functions
import django_db_views.operations
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ("meta_reports", "0059_alter_endpoints_created_and_more"),
    ]

    operations = [
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.glucosesummaryview' as `report_model` from (SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, NULL AS `ogtt_value`, NULL AS `ogtt_units`, NULL AS `ogtt_datetime`, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS `fasted`, fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucosefbg' AS source FROM meta_subject_glucosefbg AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier UNION SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, ogtt_value, ogtt_units, ogtt_datetime, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS `fasted`, fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucose' AS source FROM meta_subject_glucose AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "meta_reports_glucosesummaryview",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.glucosesummaryview' as `report_model` from (SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, NULL AS `ogtt_value`, NULL AS `ogtt_units`, NULL AS `ogtt_datetime`, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS `fasted`, fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucosefbg' AS source FROM meta_subject_glucosefbg AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier UNION SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, ogtt_value, ogtt_units, ogtt_datetime, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS `fasted`, fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucose' AS source FROM meta_subject_glucose AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "meta_reports_glucosesummaryview",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'meta_reports.glucosesummaryview' as report_model from (SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, NULL AS \"ogtt_value\", NULL AS \"ogtt_units\", NULL AS \"ogtt_datetime\", CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucosefbg' AS source FROM meta_subject_glucosefbg AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier UNION SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, ogtt_value, ogtt_units, ogtt_datetime, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucose' AS source FROM meta_subject_glucose AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "meta_reports_glucosesummaryview",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'meta_reports.glucosesummaryview' as report_model from (SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, NULL AS \"ogtt_value\", NULL AS \"ogtt_units\", NULL AS \"ogtt_datetime\", CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucosefbg' AS source FROM meta_subject_glucosefbg AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier UNION SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, ogtt_value, ogtt_units, ogtt_datetime, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucose' AS source FROM meta_subject_glucose AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "meta_reports_glucosesummaryview",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'meta_reports.glucosesummaryview' as report_model from (SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, NULL AS \"ogtt_value\", NULL AS \"ogtt_units\", NULL AS \"ogtt_datetime\", CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucosefbg' AS source FROM meta_subject_glucosefbg AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier UNION SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, ogtt_value, ogtt_units, ogtt_datetime, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucose' AS source FROM meta_subject_glucose AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "meta_reports_glucosesummaryview",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'meta_reports.glucosesummaryview' as report_model from (SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, NULL AS \"ogtt_value\", NULL AS \"ogtt_units\", NULL AS \"ogtt_datetime\", CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucosefbg' AS source FROM meta_subject_glucosefbg AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier UNION SELECT v.subject_identifier, fbg_value, fbg_units, fbg_datetime, ogtt_value, ogtt_units, ogtt_datetime, CASE WHEN fasting = 'fasting' THEN 'Yes' WHEN fasting = 'non_fasting' THEN 'No' ELSE fasting END AS \"fasted\", fbg.site_id, v.visit_code, v.visit_code_sequence, v.report_datetime, v.appointment_id, eos.offstudy_datetime, fasting_duration_delta, 'meta_subject.glucose' AS source FROM meta_subject_glucose AS fbg LEFT JOIN meta_subject_subjectvisit AS v ON v.id = fbg.subject_visit_id LEFT JOIN meta_prn_endofstudy AS eos ON v.subject_identifier = eos.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "meta_reports_glucosesummaryview",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.imp_subjectitutions_view' as `report_model` from (SELECT s.subject_identifier, s.sid, s.dispensed_sid, arm_match, s.report_datetime, r.allocated_datetime, s.site_id, s.user_created, s.user_modified, s.modified, s.id AS original_id FROM meta_pharmacy_substitutions AS s LEFT JOIN meta_rando_randomizationlist AS r ON r.subject_identifier = s.subject_identifier ORDER BY s.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "imp_subjectitutions_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.imp_subjectitutions_view' as `report_model` from (SELECT s.subject_identifier, s.sid, s.dispensed_sid, arm_match, s.report_datetime, r.allocated_datetime, s.site_id, s.user_created, s.user_modified, s.modified, s.id AS original_id FROM meta_pharmacy_substitutions AS s LEFT JOIN meta_rando_randomizationlist AS r ON r.subject_identifier = s.subject_identifier ORDER BY s.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "imp_subjectitutions_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'meta_reports.imp_subjectitutions_view' as report_model from (SELECT s.subject_identifier, s.sid, s.dispensed_sid, arm_match, s.report_datetime, r.allocated_datetime, s.site_id, s.user_created, s.user_modified, s.modified, s.id AS original_id FROM meta_pharmacy_substitutions AS s LEFT JOIN meta_rando_randomizationlist AS r ON r.subject_identifier = s.subject_identifier ORDER BY s.subject_identifier NULLS FIRST) as A ORDER BY subject_identifier, site_id",
                "imp_subjectitutions_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'meta_reports.imp_subjectitutions_view' as report_model from (SELECT s.subject_identifier, s.sid, s.dispensed_sid, arm_match, s.report_datetime, r.allocated_datetime, s.site_id, s.user_created, s.user_modified, s.modified, s.id AS original_id FROM meta_pharmacy_substitutions AS s LEFT JOIN meta_rando_randomizationlist AS r ON r.subject_identifier = s.subject_identifier ORDER BY s.subject_identifier NULLS FIRST) as A ORDER BY subject_identifier, site_id",
                "imp_subjectitutions_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'meta_reports.imp_subjectitutions_view' as report_model from (SELECT s.subject_identifier, s.sid, s.dispensed_sid, arm_match, s.report_datetime, r.allocated_datetime, s.site_id, s.user_created, s.user_modified, s.modified, s.id AS original_id FROM meta_pharmacy_substitutions AS s LEFT JOIN meta_rando_randomizationlist AS r ON r.subject_identifier = s.subject_identifier ORDER BY s.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "imp_subjectitutions_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'meta_reports.imp_subjectitutions_view' as report_model from (SELECT s.subject_identifier, s.sid, s.dispensed_sid, arm_match, s.report_datetime, r.allocated_datetime, s.site_id, s.user_created, s.user_modified, s.modified, s.id AS original_id FROM meta_pharmacy_substitutions AS s LEFT JOIN meta_rando_randomizationlist AS r ON r.subject_identifier = s.subject_identifier ORDER BY s.subject_identifier) as A ORDER BY subject_identifier, site_id",
                "imp_subjectitutions_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.missing_screening_ogtt_view' as `report_model` from (SELECT screening_identifier, site_id, report_datetime AS `screening_datetime`, fbg_datetime, converted_fbg_value AS `fbg_value`, converted_ogtt_value AS `ogtt_value`, fbg2_value, ogtt2_value, repeat_glucose_performed AS `repeated`, p3_ltfu, fbg2_datetime, ogtt2_datetime, consented, screening_identifier AS `subject_identifier`, id AS `original_id` FROM meta_screening_subjectscreening WHERE NOT converted_fbg_value IS NULL AND converted_ogtt_value IS NULL AND unsuitable_agreed <> 'Yes') as A ORDER BY subject_identifier, site_id",
                "missing_screening_ogtt_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.missing_screening_ogtt_view' as `report_model` from (SELECT screening_identifier, site_id, report_datetime AS `screening_datetime`, fbg_datetime, converted_fbg_value AS `fbg_value`, converted_ogtt_value AS `ogtt_value`, fbg2_value, ogtt2_value, repeat_glucose_performed AS `repeated`, p3_ltfu, fbg2_datetime, ogtt2_datetime, consented, screening_identifier AS `subject_identifier`, id AS `original_id` FROM meta_screening_subjectscreening WHERE NOT converted_fbg_value IS NULL AND converted_ogtt_value IS NULL AND unsuitable_agreed <> 'Yes') as A ORDER BY subject_identifier, site_id",
                "missing_screening_ogtt_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'select *, get_random_uuid() as id, now() as created, \'meta_reports.missing_screening_ogtt_view\' as report_model from (SELECT screening_identifier, site_id, report_datetime AS "screening_datetime", fbg_datetime, converted_fbg_value AS "fbg_value", converted_ogtt_value AS "ogtt_value", fbg2_value, ogtt2_value, repeat_glucose_performed AS "repeated", p3_ltfu, fbg2_datetime, ogtt2_datetime, consented, screening_identifier AS "subject_identifier", id AS "original_id" FROM meta_screening_subjectscreening WHERE NOT converted_fbg_value IS NULL AND converted_ogtt_value IS NULL AND unsuitable_agreed <> \'Yes\') as A ORDER BY subject_identifier, site_id',
                "missing_screening_ogtt_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'select *, get_random_uuid() as id, now() as created, \'meta_reports.missing_screening_ogtt_view\' as report_model from (SELECT screening_identifier, site_id, report_datetime AS "screening_datetime", fbg_datetime, converted_fbg_value AS "fbg_value", converted_ogtt_value AS "ogtt_value", fbg2_value, ogtt2_value, repeat_glucose_performed AS "repeated", p3_ltfu, fbg2_datetime, ogtt2_datetime, consented, screening_identifier AS "subject_identifier", id AS "original_id" FROM meta_screening_subjectscreening WHERE NOT converted_fbg_value IS NULL AND converted_ogtt_value IS NULL AND unsuitable_agreed <> \'Yes\') as A ORDER BY subject_identifier, site_id',
                "missing_screening_ogtt_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'select *, uuid() as id, datetime() as created, \'meta_reports.missing_screening_ogtt_view\' as report_model from (SELECT screening_identifier, site_id, report_datetime AS "screening_datetime", fbg_datetime, converted_fbg_value AS "fbg_value", converted_ogtt_value AS "ogtt_value", fbg2_value, ogtt2_value, repeat_glucose_performed AS "repeated", p3_ltfu, fbg2_datetime, ogtt2_datetime, consented, screening_identifier AS "subject_identifier", id AS "original_id" FROM meta_screening_subjectscreening WHERE NOT converted_fbg_value IS NULL AND converted_ogtt_value IS NULL AND unsuitable_agreed <> \'Yes\') as A ORDER BY subject_identifier, site_id',
                "missing_screening_ogtt_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'select *, uuid() as id, datetime() as created, \'meta_reports.missing_screening_ogtt_view\' as report_model from (SELECT screening_identifier, site_id, report_datetime AS "screening_datetime", fbg_datetime, converted_fbg_value AS "fbg_value", converted_ogtt_value AS "ogtt_value", fbg2_value, ogtt2_value, repeat_glucose_performed AS "repeated", p3_ltfu, fbg2_datetime, ogtt2_datetime, consented, screening_identifier AS "subject_identifier", id AS "original_id" FROM meta_screening_subjectscreening WHERE NOT converted_fbg_value IS NULL AND converted_ogtt_value IS NULL AND unsuitable_agreed <> \'Yes\') as A ORDER BY subject_identifier, site_id',
                "missing_screening_ogtt_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'onstudy_missing_lab_values_view' as `report_model` from (SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsfbc AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'fbc' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsrft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'RFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslipids AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LIPIDS' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing haemoglobin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.haemoglobin_value IS NULL OR crf.haemoglobin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing hct value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hct_value IS NULL OR crf.hct_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing rbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.rbc_value IS NULL OR crf.rbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing wbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.wbc_value IS NULL OR crf.wbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing platelets value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.platelets_value IS NULL OR crf.platelets_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mcv value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mcv_value IS NULL OR crf.mcv_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mch value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mch_value IS NULL OR crf.mch_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mchc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mchc_value IS NULL OR crf.mchc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing urea value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.urea_value IS NULL OR crf.urea_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing creatinine value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.creatinine_value IS NULL OR crf.creatinine_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing uric_acid value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.uric_acid_value IS NULL OR crf.uric_acid_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_value IS NULL OR crf.egfr_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr_drop value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_drop_value IS NULL OR crf.egfr_drop_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ast value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ast_value IS NULL OR crf.ast_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alt_value IS NULL OR crf.alt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alp value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alp_value IS NULL OR crf.alp_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing amylase value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.amylase_value IS NULL OR crf.amylase_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ggt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ggt_value IS NULL OR crf.ggt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing albumin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.albumin_value IS NULL OR crf.albumin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing ldl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ldl_value IS NULL OR crf.ldl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing hdl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hdl_value IS NULL OR crf.hdl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing trig value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.trig_value IS NULL OR crf.trig_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing chol value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.chol_value IS NULL OR crf.chol_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_lab_values_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'onstudy_missing_lab_values_view' as `report_model` from (SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsfbc AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'fbc' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsrft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'RFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslipids AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LIPIDS' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing haemoglobin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.haemoglobin_value IS NULL OR crf.haemoglobin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing hct value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hct_value IS NULL OR crf.hct_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing rbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.rbc_value IS NULL OR crf.rbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing wbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.wbc_value IS NULL OR crf.wbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing platelets value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.platelets_value IS NULL OR crf.platelets_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mcv value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mcv_value IS NULL OR crf.mcv_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mch value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mch_value IS NULL OR crf.mch_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mchc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mchc_value IS NULL OR crf.mchc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing urea value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.urea_value IS NULL OR crf.urea_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing creatinine value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.creatinine_value IS NULL OR crf.creatinine_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing uric_acid value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.uric_acid_value IS NULL OR crf.uric_acid_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_value IS NULL OR crf.egfr_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr_drop value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_drop_value IS NULL OR crf.egfr_drop_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ast value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ast_value IS NULL OR crf.ast_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alt_value IS NULL OR crf.alt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alp value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alp_value IS NULL OR crf.alp_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing amylase value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.amylase_value IS NULL OR crf.amylase_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ggt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ggt_value IS NULL OR crf.ggt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing albumin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.albumin_value IS NULL OR crf.albumin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing ldl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ldl_value IS NULL OR crf.ldl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing hdl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hdl_value IS NULL OR crf.hdl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing trig value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.trig_value IS NULL OR crf.trig_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing chol value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.chol_value IS NULL OR crf.chol_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_lab_values_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'onstudy_missing_lab_values_view' as report_model from (SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsfbc AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'fbc' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsrft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'RFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslipids AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LIPIDS' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing haemoglobin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.haemoglobin_value IS NULL OR crf.haemoglobin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing hct value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hct_value IS NULL OR crf.hct_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing rbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.rbc_value IS NULL OR crf.rbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing wbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.wbc_value IS NULL OR crf.wbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing platelets value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.platelets_value IS NULL OR crf.platelets_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mcv value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mcv_value IS NULL OR crf.mcv_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mch value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mch_value IS NULL OR crf.mch_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mchc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mchc_value IS NULL OR crf.mchc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing urea value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.urea_value IS NULL OR crf.urea_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing creatinine value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.creatinine_value IS NULL OR crf.creatinine_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing uric_acid value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.uric_acid_value IS NULL OR crf.uric_acid_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_value IS NULL OR crf.egfr_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr_drop value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_drop_value IS NULL OR crf.egfr_drop_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ast value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ast_value IS NULL OR crf.ast_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alt_value IS NULL OR crf.alt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alp value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alp_value IS NULL OR crf.alp_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing amylase value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.amylase_value IS NULL OR crf.amylase_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ggt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ggt_value IS NULL OR crf.ggt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing albumin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.albumin_value IS NULL OR crf.albumin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing ldl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ldl_value IS NULL OR crf.ldl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing hdl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hdl_value IS NULL OR crf.hdl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing trig value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.trig_value IS NULL OR crf.trig_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing chol value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.chol_value IS NULL OR crf.chol_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_lab_values_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'onstudy_missing_lab_values_view' as report_model from (SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsfbc AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'fbc' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsrft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'RFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslipids AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LIPIDS' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing haemoglobin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.haemoglobin_value IS NULL OR crf.haemoglobin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing hct value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hct_value IS NULL OR crf.hct_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing rbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.rbc_value IS NULL OR crf.rbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing wbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.wbc_value IS NULL OR crf.wbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing platelets value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.platelets_value IS NULL OR crf.platelets_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mcv value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mcv_value IS NULL OR crf.mcv_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mch value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mch_value IS NULL OR crf.mch_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mchc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mchc_value IS NULL OR crf.mchc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing urea value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.urea_value IS NULL OR crf.urea_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing creatinine value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.creatinine_value IS NULL OR crf.creatinine_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing uric_acid value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.uric_acid_value IS NULL OR crf.uric_acid_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_value IS NULL OR crf.egfr_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr_drop value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_drop_value IS NULL OR crf.egfr_drop_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ast value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ast_value IS NULL OR crf.ast_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alt_value IS NULL OR crf.alt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alp value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alp_value IS NULL OR crf.alp_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing amylase value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.amylase_value IS NULL OR crf.amylase_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ggt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ggt_value IS NULL OR crf.ggt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing albumin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.albumin_value IS NULL OR crf.albumin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing ldl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ldl_value IS NULL OR crf.ldl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing hdl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hdl_value IS NULL OR crf.hdl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing trig value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.trig_value IS NULL OR crf.trig_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing chol value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.chol_value IS NULL OR crf.chol_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_lab_values_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'onstudy_missing_lab_values_view' as report_model from (SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsfbc AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'fbc' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsrft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'RFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslipids AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LIPIDS' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing haemoglobin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.haemoglobin_value IS NULL OR crf.haemoglobin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing hct value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hct_value IS NULL OR crf.hct_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing rbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.rbc_value IS NULL OR crf.rbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing wbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.wbc_value IS NULL OR crf.wbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing platelets value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.platelets_value IS NULL OR crf.platelets_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mcv value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mcv_value IS NULL OR crf.mcv_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mch value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mch_value IS NULL OR crf.mch_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mchc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mchc_value IS NULL OR crf.mchc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing urea value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.urea_value IS NULL OR crf.urea_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing creatinine value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.creatinine_value IS NULL OR crf.creatinine_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing uric_acid value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.uric_acid_value IS NULL OR crf.uric_acid_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_value IS NULL OR crf.egfr_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr_drop value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_drop_value IS NULL OR crf.egfr_drop_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ast value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ast_value IS NULL OR crf.ast_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alt_value IS NULL OR crf.alt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alp value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alp_value IS NULL OR crf.alp_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing amylase value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.amylase_value IS NULL OR crf.amylase_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ggt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ggt_value IS NULL OR crf.ggt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing albumin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.albumin_value IS NULL OR crf.albumin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing ldl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ldl_value IS NULL OR crf.ldl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing hdl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hdl_value IS NULL OR crf.hdl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing trig value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.trig_value IS NULL OR crf.trig_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing chol value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.chol_value IS NULL OR crf.chol_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_lab_values_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'onstudy_missing_lab_values_view' as report_model from (SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsfbc AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'fbc' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultsrft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'RFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslft AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LFT' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT req.subject_identifier, req.id AS original_id, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS requisitioned but not entered' AS label, COUNT(*) AS records FROM meta_subject_subjectrequisition AS req LEFT JOIN meta_subject_bloodresultslipids AS crf ON req.id = crf.requisition_id LEFT JOIN meta_subject_subjectvisit AS v ON v.id = req.subject_visit_id LEFT JOIN edc_lab_panel AS panel ON req.panel_id = panel.id WHERE panel.name = 'LIPIDS' AND req.is_drawn = 'Yes' AND crf.id IS NULL GROUP BY req.id, req.subject_identifier, req.subject_visit_id, req.report_datetime, req.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, req.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing haemoglobin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.haemoglobin_value IS NULL OR crf.haemoglobin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing hct value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hct_value IS NULL OR crf.hct_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing rbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.rbc_value IS NULL OR crf.rbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing wbc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.wbc_value IS NULL OR crf.wbc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing platelets value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.platelets_value IS NULL OR crf.platelets_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mcv value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mcv_value IS NULL OR crf.mcv_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mch value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mch_value IS NULL OR crf.mch_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsfbc' AS label_lower, 'FBC: missing mchc value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsfbc AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.mchc_value IS NULL OR crf.mchc_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing urea value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.urea_value IS NULL OR crf.urea_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing creatinine value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.creatinine_value IS NULL OR crf.creatinine_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing uric_acid value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.uric_acid_value IS NULL OR crf.uric_acid_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_value IS NULL OR crf.egfr_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultsrft' AS label_lower, 'RFT: missing egfr_drop value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultsrft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.egfr_drop_value IS NULL OR crf.egfr_drop_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ast value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ast_value IS NULL OR crf.ast_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alt_value IS NULL OR crf.alt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing alp value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.alp_value IS NULL OR crf.alp_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing amylase value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.amylase_value IS NULL OR crf.amylase_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing ggt value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ggt_value IS NULL OR crf.ggt_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslft' AS label_lower, 'LFT: missing albumin value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslft AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.albumin_value IS NULL OR crf.albumin_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing ldl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.ldl_value IS NULL OR crf.ldl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing hdl value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hdl_value IS NULL OR crf.hdl_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing trig value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.trig_value IS NULL OR crf.trig_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.bloodresultslipids' AS label_lower, 'LIPIDS: missing chol value/units' AS label, COUNT(*) AS records FROM meta_subject_bloodresultslipids AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.chol_value IS NULL OR crf.chol_units IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_lab_values_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'onstudy_missing_values_view' as `report_model` from (SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No HIV Diagnosis date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hiv_diagnosis_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No VL value or VL date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.viral_load IS NULL OR crf.viral_load_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No CD4 value or CD4 date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.cd4 IS NULL OR crf.cd4_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date but previous ARV is YES' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.has_previous_arv_regimen = 'Yes' AND crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'Other current ARV regimen missing' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id LEFT JOIN meta_lists_arvregimens AS arvregimen ON crf.current_arv_regimen_id = arvregimen.id WHERE crf.other_current_arv_regimen IS NULL AND arvregimen.name = 'OTHER' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.glucose' AS label_lower, 'FBG/OGTT: missing OGTT' AS label, COUNT(*) AS records FROM meta_subject_glucose AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (NOT crf.fbg_value IS NULL AND crf.ogtt_value IS NULL) AND ogtt_performed = 'Yes' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.followupvitals' AS label_lower, 'Weight' AS label, COUNT(*) AS records FROM meta_subject_followupvitals AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (crf.weight IS NULL AND v.visit_code_sequence = 0) GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.followupvitals' AS label_lower, 'Waist circumference 36/48m' AS label, COUNT(*) AS records FROM meta_subject_followupvitals AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (crf.waist_circumference_measured = 'Yes' AND crf.waist_circumference IS NULL) OR (crf.waist_circumference IS NULL AND v.visit_code IN ('1360', '1480') AND v.visit_code_sequence = 0) GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_values_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'onstudy_missing_values_view' as `report_model` from (SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No HIV Diagnosis date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hiv_diagnosis_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No VL value or VL date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.viral_load IS NULL OR crf.viral_load_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No CD4 value or CD4 date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.cd4 IS NULL OR crf.cd4_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date but previous ARV is YES' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.has_previous_arv_regimen = 'Yes' AND crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'Other current ARV regimen missing' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id LEFT JOIN meta_lists_arvregimens AS arvregimen ON crf.current_arv_regimen_id = arvregimen.id WHERE crf.other_current_arv_regimen IS NULL AND arvregimen.name = 'OTHER' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.glucose' AS label_lower, 'FBG/OGTT: missing OGTT' AS label, COUNT(*) AS records FROM meta_subject_glucose AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (NOT crf.fbg_value IS NULL AND crf.ogtt_value IS NULL) AND ogtt_performed = 'Yes' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_values_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'onstudy_missing_values_view' as report_model from (SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No HIV Diagnosis date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hiv_diagnosis_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No VL value or VL date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.viral_load IS NULL OR crf.viral_load_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No CD4 value or CD4 date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.cd4 IS NULL OR crf.cd4_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date but previous ARV is YES' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.has_previous_arv_regimen = 'Yes' AND crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'Other current ARV regimen missing' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id LEFT JOIN meta_lists_arvregimens AS arvregimen ON crf.current_arv_regimen_id = arvregimen.id WHERE crf.other_current_arv_regimen IS NULL AND arvregimen.name = 'OTHER' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.glucose' AS label_lower, 'FBG/OGTT: missing OGTT' AS label, COUNT(*) AS records FROM meta_subject_glucose AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (NOT crf.fbg_value IS NULL AND crf.ogtt_value IS NULL) AND ogtt_performed = 'Yes' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.followupvitals' AS label_lower, 'Weight' AS label, COUNT(*) AS records FROM meta_subject_followupvitals AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (crf.weight IS NULL AND v.visit_code_sequence = 0) GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.followupvitals' AS label_lower, 'Waist circumference 36/48m' AS label, COUNT(*) AS records FROM meta_subject_followupvitals AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (crf.waist_circumference_measured = 'Yes' AND crf.waist_circumference IS NULL) OR (crf.waist_circumference IS NULL AND v.visit_code IN ('1360', '1480') AND v.visit_code_sequence = 0) GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_values_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'onstudy_missing_values_view' as report_model from (SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No HIV Diagnosis date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hiv_diagnosis_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No VL value or VL date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.viral_load IS NULL OR crf.viral_load_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No CD4 value or CD4 date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.cd4 IS NULL OR crf.cd4_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date but previous ARV is YES' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.has_previous_arv_regimen = 'Yes' AND crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'Other current ARV regimen missing' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id LEFT JOIN meta_lists_arvregimens AS arvregimen ON crf.current_arv_regimen_id = arvregimen.id WHERE crf.other_current_arv_regimen IS NULL AND arvregimen.name = 'OTHER' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.glucose' AS label_lower, 'FBG/OGTT: missing OGTT' AS label, COUNT(*) AS records FROM meta_subject_glucose AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (NOT crf.fbg_value IS NULL AND crf.ogtt_value IS NULL) AND ogtt_performed = 'Yes' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_values_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'onstudy_missing_values_view' as report_model from (SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No HIV Diagnosis date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hiv_diagnosis_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No VL value or VL date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.viral_load IS NULL OR crf.viral_load_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No CD4 value or CD4 date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.cd4 IS NULL OR crf.cd4_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date but previous ARV is YES' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.has_previous_arv_regimen = 'Yes' AND crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'Other current ARV regimen missing' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id LEFT JOIN meta_lists_arvregimens AS arvregimen ON crf.current_arv_regimen_id = arvregimen.id WHERE crf.other_current_arv_regimen IS NULL AND arvregimen.name = 'OTHER' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.glucose' AS label_lower, 'FBG/OGTT: missing OGTT' AS label, COUNT(*) AS records FROM meta_subject_glucose AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (NOT crf.fbg_value IS NULL AND crf.ogtt_value IS NULL) AND ogtt_performed = 'Yes' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.followupvitals' AS label_lower, 'Weight' AS label, COUNT(*) AS records FROM meta_subject_followupvitals AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (crf.weight IS NULL AND v.visit_code_sequence = 0) GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.followupvitals' AS label_lower, 'Waist circumference 36/48m' AS label, COUNT(*) AS records FROM meta_subject_followupvitals AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (crf.waist_circumference_measured = 'Yes' AND crf.waist_circumference IS NULL) OR (crf.waist_circumference IS NULL AND v.visit_code IN ('1360', '1480') AND v.visit_code_sequence = 0) GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_values_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'onstudy_missing_values_view' as report_model from (SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No HIV Diagnosis date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.hiv_diagnosis_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No VL value or VL date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.viral_load IS NULL OR crf.viral_load_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No CD4 value or CD4 date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.cd4 IS NULL OR crf.cd4_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'No current ARV start date but previous ARV is YES' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE crf.has_previous_arv_regimen = 'Yes' AND crf.current_arv_regimen_start_date IS NULL GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.patienthistory' AS label_lower, 'Other current ARV regimen missing' AS label, COUNT(*) AS records FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id LEFT JOIN meta_lists_arvregimens AS arvregimen ON crf.current_arv_regimen_id = arvregimen.id WHERE crf.other_current_arv_regimen IS NULL AND arvregimen.name = 'OTHER' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified UNION SELECT v.subject_identifier, crf.id AS original_id, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified, 'meta_subject.glucose' AS label_lower, 'FBG/OGTT: missing OGTT' AS label, COUNT(*) AS records FROM meta_subject_glucose AS crf LEFT JOIN meta_subject_subjectvisit AS v ON v.id = crf.subject_visit_id WHERE (NOT crf.fbg_value IS NULL AND crf.ogtt_value IS NULL) AND ogtt_performed = 'Yes' GROUP BY v.subject_identifier, crf.subject_visit_id, crf.report_datetime, crf.site_id, v.visit_code, v.visit_code_sequence, v.schedule_name, crf.modified) as A ORDER BY subject_identifier, site_id",
                "onstudy_missing_values_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.patienthistorymissingbaselinecd4' as `report_model` from (SELECT subject_identifier, v.visit_code, v.visit_code_sequence, cd4, cd4_date, crf.site_id, crf.user_created, crf.user_modified, crf.modified FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON crf.subject_visit_id = v.id WHERE cd4 IS NULL OR cd4_date IS NULL) as A ORDER BY subject_identifier, site_id",
                "patient_history_missing_baseline_cd4_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.patienthistorymissingbaselinecd4' as `report_model` from (SELECT subject_identifier, v.visit_code, v.visit_code_sequence, cd4, cd4_date, crf.site_id, crf.user_created, crf.user_modified, crf.modified FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON crf.subject_visit_id = v.id WHERE cd4 IS NULL OR cd4_date IS NULL) as A ORDER BY subject_identifier, site_id",
                "patient_history_missing_baseline_cd4_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'meta_reports.patienthistorymissingbaselinecd4' as report_model from (SELECT subject_identifier, v.visit_code, v.visit_code_sequence, cd4, cd4_date, crf.site_id, crf.user_created, crf.user_modified, crf.modified FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON crf.subject_visit_id = v.id WHERE cd4 IS NULL OR cd4_date IS NULL) as A ORDER BY subject_identifier, site_id",
                "patient_history_missing_baseline_cd4_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, get_random_uuid() as id, now() as created, 'meta_reports.patienthistorymissingbaselinecd4' as report_model from (SELECT subject_identifier, v.visit_code, v.visit_code_sequence, cd4, cd4_date, crf.site_id, crf.user_created, crf.user_modified, crf.modified FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON crf.subject_visit_id = v.id WHERE cd4 IS NULL OR cd4_date IS NULL) as A ORDER BY subject_identifier, site_id",
                "patient_history_missing_baseline_cd4_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'meta_reports.patienthistorymissingbaselinecd4' as report_model from (SELECT subject_identifier, v.visit_code, v.visit_code_sequence, cd4, cd4_date, crf.site_id, crf.user_created, crf.user_modified, crf.modified FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON crf.subject_visit_id = v.id WHERE cd4 IS NULL OR cd4_date IS NULL) as A ORDER BY subject_identifier, site_id",
                "patient_history_missing_baseline_cd4_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, datetime() as created, 'meta_reports.patienthistorymissingbaselinecd4' as report_model from (SELECT subject_identifier, v.visit_code, v.visit_code_sequence, cd4, cd4_date, crf.site_id, crf.user_created, crf.user_modified, crf.modified FROM meta_subject_patienthistory AS crf LEFT JOIN meta_subject_subjectvisit AS v ON crf.subject_visit_id = v.id WHERE cd4 IS NULL OR cd4_date IS NULL) as A ORDER BY subject_identifier, site_id",
                "patient_history_missing_baseline_cd4_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.unattendedthreeinrow' as `report_model` from (SELECT subject_identifier, site_id, appt_datetime, `first_value`, `second_value`, `third_value`, DATEDIFF(`third_date`, `first_date`) AS `interval_days`, DATEDIFF(NOW(), `first_date`) AS `from_now_days` FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS `first_value`, NTH_VALUE(visit_code, 2) OVER w AS `second_value`, NTH_VALUE(visit_code, 3) OVER w AS `third_value`, FIRST_VALUE(appt_datetime) OVER w AS `first_date`, NTH_VALUE(appt_datetime, 3) OVER w AS `third_date` FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = 'New' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT `second_value` IS NULL AND NOT `third_value` IS NULL) as A ORDER BY subject_identifier, site_id",
                "unattended_three_in_row_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.unattendedthreeinrow' as `report_model` from (SELECT subject_identifier, site_id, appt_datetime, `first_value`, `second_value`, `third_value`, DATEDIFF(`third_date`, `first_date`) AS `interval_days`, DATEDIFF(NOW(), `first_date`) AS `from_now_days` FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS `first_value`, NTH_VALUE(visit_code, 2) OVER w AS `second_value`, NTH_VALUE(visit_code, 3) OVER w AS `third_value`, FIRST_VALUE(appt_datetime) OVER w AS `first_date`, NTH_VALUE(appt_datetime, 3) OVER w AS `third_date` FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = 'New' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT `second_value` IS NULL AND NOT `third_value` IS NULL) as A ORDER BY subject_identifier, site_id",
                "unattended_three_in_row_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'select *, get_random_uuid() as id, now() as created, \'meta_reports.unattendedthreeinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", "third_value", CAST(AGE(CAST("third_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS "interval_days", CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS "from_now_days" FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 3) OVER w AS "third_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'select *, get_random_uuid() as id, now() as created, \'meta_reports.unattendedthreeinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", "third_value", CAST(AGE(CAST("third_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS "interval_days", CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS "from_now_days" FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 3) OVER w AS "third_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'select *, uuid() as id, datetime() as created, \'meta_reports.unattendedthreeinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", "third_value", CAST((JULIANDAY("third_date") - JULIANDAY("first_date")) AS INTEGER) AS "interval_days", CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER) AS "from_now_days" FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 3) OVER w AS "third_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'select *, uuid() as id, datetime() as created, \'meta_reports.unattendedthreeinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", "third_value", CAST((JULIANDAY("third_date") - JULIANDAY("first_date")) AS INTEGER) AS "interval_days", CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER) AS "from_now_days" FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 3) OVER w AS "third_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "with appointments as (select subject_identifier, site_id, visit_code, visit_code_sequence,\nappt_datetime, case when appt_timing=\"missed\" then \"New\" else appt_status end as `appt_status`,\ncase when appt_timing=\"missed\" then 1 else 0 end as `missed`\nfrom edc_appointment_appointment\nwhere visit_code_sequence=0 and appt_datetime<=now()\norder by subject_identifier, appt_datetime) select *, uuid() as id, now() as `created`, 'meta_reports.unattendedthreeinrow2' as `report_model` from (SELECT DISTINCT subject_identifier, site_id, `first_value`, `second_value`, `third_value`, DATEDIFF(`third_date`, `first_date`) AS interval_days, DATEDIFF(NOW(), `first_date`) AS from_now_days, `first_status`, `second_status`, `third_status`, SUM(missed) AS missed_count FROM (SELECT subject_identifier, site_id, appt_datetime, `missed`, FIRST_VALUE(appt_status) OVER w AS `third_status`, NTH_VALUE(appt_status, 2) OVER w AS `second_status`, NTH_VALUE(appt_status, 3) OVER w AS `first_status`, FIRST_VALUE(visit_code) OVER w AS `third_value`, NTH_VALUE(visit_code, 2) OVER w AS `second_value`, NTH_VALUE(visit_code, 3) OVER w AS `first_value`, FIRST_VALUE(appt_datetime) OVER w AS `third_date`, NTH_VALUE(appt_datetime, 2) OVER w AS `second_date`, NTH_VALUE(appt_datetime, 3) OVER w AS `first_date` FROM appointments WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT `second_value` IS NULL AND NOT `third_value` IS NULL AND `first_status` = 'New' AND `second_status` = 'New' AND `third_status` = 'New' GROUP BY subject_identifier, site_id, `first_value`, `second_value`, `third_value`, DATEDIFF(`third_date`, `first_date`), DATEDIFF(NOW(), `first_date`), `first_status`, `second_status`, `third_status` ORDER BY subject_identifier, site_id) as A ORDER BY subject_identifier, site_id",
                "unattended_three_in_row2_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "with appointments as (select subject_identifier, site_id, visit_code, visit_code_sequence,\nappt_datetime, case when appt_timing=\"missed\" then \"New\" else appt_status end as `appt_status`,\ncase when appt_timing=\"missed\" then 1 else 0 end as `missed`\nfrom edc_appointment_appointment\nwhere visit_code_sequence=0 and appt_datetime<=now()\norder by subject_identifier, appt_datetime) select *, uuid() as id, now() as `created`, 'meta_reports.unattendedthreeinrow2' as `report_model` from (SELECT DISTINCT subject_identifier, site_id, `first_value`, `second_value`, `third_value`, DATEDIFF(`third_date`, `first_date`) AS interval_days, DATEDIFF(NOW(), `first_date`) AS from_now_days, `first_status`, `second_status`, `third_status`, SUM(missed) AS missed_count FROM (SELECT subject_identifier, site_id, appt_datetime, `missed`, FIRST_VALUE(appt_status) OVER w AS `third_status`, NTH_VALUE(appt_status, 2) OVER w AS `second_status`, NTH_VALUE(appt_status, 3) OVER w AS `first_status`, FIRST_VALUE(visit_code) OVER w AS `third_value`, NTH_VALUE(visit_code, 2) OVER w AS `second_value`, NTH_VALUE(visit_code, 3) OVER w AS `first_value`, FIRST_VALUE(appt_datetime) OVER w AS `third_date`, NTH_VALUE(appt_datetime, 2) OVER w AS `second_date`, NTH_VALUE(appt_datetime, 3) OVER w AS `first_date` FROM appointments WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT `second_value` IS NULL AND NOT `third_value` IS NULL AND `first_status` = 'New' AND `second_status` = 'New' AND `third_status` = 'New' GROUP BY subject_identifier, site_id, `first_value`, `second_value`, `third_value`, DATEDIFF(`third_date`, `first_date`), DATEDIFF(NOW(), `first_date`), `first_status`, `second_status`, `third_status` ORDER BY subject_identifier, site_id) as A ORDER BY subject_identifier, site_id",
                "unattended_three_in_row2_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'with appointments as (select subject_identifier, site_id, visit_code, visit_code_sequence,\nappt_datetime, case when appt_timing="missed" then "New" else appt_status end as `appt_status`,\ncase when appt_timing="missed" then 1 else 0 end as `missed`\nfrom edc_appointment_appointment\nwhere visit_code_sequence=0 and appt_datetime<=now()\norder by subject_identifier, appt_datetime) select *, get_random_uuid() as id, now() as created, \'meta_reports.unattendedthreeinrow2\' as report_model from (SELECT DISTINCT subject_identifier, site_id, "first_value", "second_value", "third_value", CAST(AGE(CAST("third_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS interval_days, CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS from_now_days, "first_status", "second_status", "third_status", SUM(missed) AS missed_count FROM (SELECT subject_identifier, site_id, appt_datetime, "missed", FIRST_VALUE(appt_status) OVER w AS "third_status", NTH_VALUE(appt_status, 2) OVER w AS "second_status", NTH_VALUE(appt_status, 3) OVER w AS "first_status", FIRST_VALUE(visit_code) OVER w AS "third_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "first_value", FIRST_VALUE(appt_datetime) OVER w AS "third_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date", NTH_VALUE(appt_datetime, 3) OVER w AS "first_date" FROM appointments WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime DESC NULLS LAST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL AND "first_status" = \'New\' AND "second_status" = \'New\' AND "third_status" = \'New\' GROUP BY subject_identifier, site_id, "first_value", "second_value", "third_value", CAST(AGE(CAST("third_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT), CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT), "first_status", "second_status", "third_status" ORDER BY subject_identifier NULLS FIRST, site_id NULLS FIRST) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row2_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'with appointments as (select subject_identifier, site_id, visit_code, visit_code_sequence,\nappt_datetime, case when appt_timing="missed" then "New" else appt_status end as `appt_status`,\ncase when appt_timing="missed" then 1 else 0 end as `missed`\nfrom edc_appointment_appointment\nwhere visit_code_sequence=0 and appt_datetime<=now()\norder by subject_identifier, appt_datetime) select *, get_random_uuid() as id, now() as created, \'meta_reports.unattendedthreeinrow2\' as report_model from (SELECT DISTINCT subject_identifier, site_id, "first_value", "second_value", "third_value", CAST(AGE(CAST("third_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS interval_days, CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS from_now_days, "first_status", "second_status", "third_status", SUM(missed) AS missed_count FROM (SELECT subject_identifier, site_id, appt_datetime, "missed", FIRST_VALUE(appt_status) OVER w AS "third_status", NTH_VALUE(appt_status, 2) OVER w AS "second_status", NTH_VALUE(appt_status, 3) OVER w AS "first_status", FIRST_VALUE(visit_code) OVER w AS "third_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "first_value", FIRST_VALUE(appt_datetime) OVER w AS "third_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date", NTH_VALUE(appt_datetime, 3) OVER w AS "first_date" FROM appointments WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime DESC NULLS LAST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL AND "first_status" = \'New\' AND "second_status" = \'New\' AND "third_status" = \'New\' GROUP BY subject_identifier, site_id, "first_value", "second_value", "third_value", CAST(AGE(CAST("third_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT), CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT), "first_status", "second_status", "third_status" ORDER BY subject_identifier NULLS FIRST, site_id NULLS FIRST) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row2_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'with appointments as (select subject_identifier, site_id, visit_code, visit_code_sequence,\nappt_datetime, case when appt_timing="missed" then "New" else appt_status end as `appt_status`,\ncase when appt_timing="missed" then 1 else 0 end as `missed`\nfrom edc_appointment_appointment\nwhere visit_code_sequence=0 and appt_datetime<=now()\norder by subject_identifier, appt_datetime) select *, uuid() as id, datetime() as created, \'meta_reports.unattendedthreeinrow2\' as report_model from (SELECT DISTINCT subject_identifier, site_id, "first_value", "second_value", "third_value", CAST((JULIANDAY("third_date") - JULIANDAY("first_date")) AS INTEGER) AS interval_days, CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER) AS from_now_days, "first_status", "second_status", "third_status", SUM(missed) AS missed_count FROM (SELECT subject_identifier, site_id, appt_datetime, "missed", FIRST_VALUE(appt_status) OVER w AS "third_status", NTH_VALUE(appt_status, 2) OVER w AS "second_status", NTH_VALUE(appt_status, 3) OVER w AS "first_status", FIRST_VALUE(visit_code) OVER w AS "third_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "first_value", FIRST_VALUE(appt_datetime) OVER w AS "third_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date", NTH_VALUE(appt_datetime, 3) OVER w AS "first_date" FROM appointments WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL AND "first_status" = \'New\' AND "second_status" = \'New\' AND "third_status" = \'New\' GROUP BY subject_identifier, site_id, "first_value", "second_value", "third_value", CAST((JULIANDAY("third_date") - JULIANDAY("first_date")) AS INTEGER), CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER), "first_status", "second_status", "third_status" ORDER BY subject_identifier, site_id) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row2_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'with appointments as (select subject_identifier, site_id, visit_code, visit_code_sequence,\nappt_datetime, case when appt_timing="missed" then "New" else appt_status end as `appt_status`,\ncase when appt_timing="missed" then 1 else 0 end as `missed`\nfrom edc_appointment_appointment\nwhere visit_code_sequence=0 and appt_datetime<=now()\norder by subject_identifier, appt_datetime) select *, uuid() as id, datetime() as created, \'meta_reports.unattendedthreeinrow2\' as report_model from (SELECT DISTINCT subject_identifier, site_id, "first_value", "second_value", "third_value", CAST((JULIANDAY("third_date") - JULIANDAY("first_date")) AS INTEGER) AS interval_days, CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER) AS from_now_days, "first_status", "second_status", "third_status", SUM(missed) AS missed_count FROM (SELECT subject_identifier, site_id, appt_datetime, "missed", FIRST_VALUE(appt_status) OVER w AS "third_status", NTH_VALUE(appt_status, 2) OVER w AS "second_status", NTH_VALUE(appt_status, 3) OVER w AS "first_status", FIRST_VALUE(visit_code) OVER w AS "third_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "first_value", FIRST_VALUE(appt_datetime) OVER w AS "third_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date", NTH_VALUE(appt_datetime, 3) OVER w AS "first_date" FROM appointments WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND NOT "third_value" IS NULL AND "first_status" = \'New\' AND "second_status" = \'New\' AND "third_status" = \'New\' GROUP BY subject_identifier, site_id, "first_value", "second_value", "third_value", CAST((JULIANDAY("third_date") - JULIANDAY("first_date")) AS INTEGER), CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER), "first_status", "second_status", "third_status" ORDER BY subject_identifier, site_id) as A ORDER BY subject_identifier, site_id',
                "unattended_three_in_row2_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.unattendedtwoinrow' as `report_model` from (SELECT subject_identifier, site_id, appt_datetime, `first_value`, `second_value`, DATEDIFF(`second_date`, `first_date`) AS interval_days, DATEDIFF(NOW(), `first_date`) AS from_now_days FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS `first_value`, NTH_VALUE(visit_code, 2) OVER w AS `second_value`, NTH_VALUE(visit_code, 3) OVER w AS `third_value`, FIRST_VALUE(appt_datetime) OVER w AS `first_date`, NTH_VALUE(appt_datetime, 2) OVER w AS `second_date` FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = 'New' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT `second_value` IS NULL AND `third_value` IS NULL) as A ORDER BY site_id, from_now_days desc",
                "unattended_two_in_row_view",
                engine="django.db.backends.mysql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "select *, uuid() as id, now() as `created`, 'meta_reports.unattendedtwoinrow' as `report_model` from (SELECT subject_identifier, site_id, appt_datetime, `first_value`, `second_value`, DATEDIFF(`second_date`, `first_date`) AS interval_days, DATEDIFF(NOW(), `first_date`) AS from_now_days FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS `first_value`, NTH_VALUE(visit_code, 2) OVER w AS `second_value`, NTH_VALUE(visit_code, 3) OVER w AS `third_value`, FIRST_VALUE(appt_datetime) OVER w AS `first_date`, NTH_VALUE(appt_datetime, 2) OVER w AS `second_date` FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = 'New' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT `second_value` IS NULL AND `third_value` IS NULL) as A ORDER BY site_id, from_now_days desc",
                "unattended_two_in_row_view",
                engine="django.db.backends.mysql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'select *, get_random_uuid() as id, now() as created, \'meta_reports.unattendedtwoinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", CAST(AGE(CAST("second_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS interval_days, CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS from_now_days FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND "third_value" IS NULL) as A ORDER BY site_id, from_now_days desc',
                "unattended_two_in_row_view",
                engine="django.db.backends.postgresql",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'select *, get_random_uuid() as id, now() as created, \'meta_reports.unattendedtwoinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", CAST(AGE(CAST("second_date" AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS interval_days, CAST(AGE(CAST(NOW() AS TIMESTAMP), CAST("first_date" AS TIMESTAMP)) AS BIGINT) AS from_now_days FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime NULLS FIRST ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND "third_value" IS NULL) as A ORDER BY site_id, from_now_days desc',
                "unattended_two_in_row_view",
                engine="django.db.backends.postgresql",
            ),
            atomic=False,
        ),
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                'select *, uuid() as id, datetime() as created, \'meta_reports.unattendedtwoinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", CAST((JULIANDAY("second_date") - JULIANDAY("first_date")) AS INTEGER) AS interval_days, CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER) AS from_now_days FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND "third_value" IS NULL) as A ORDER BY site_id, from_now_days desc',
                "unattended_two_in_row_view",
                engine="django.db.backends.sqlite3",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                'select *, uuid() as id, datetime() as created, \'meta_reports.unattendedtwoinrow\' as report_model from (SELECT subject_identifier, site_id, appt_datetime, "first_value", "second_value", CAST((JULIANDAY("second_date") - JULIANDAY("first_date")) AS INTEGER) AS interval_days, CAST((JULIANDAY(NOW()) - JULIANDAY("first_date")) AS INTEGER) AS from_now_days FROM (SELECT subject_identifier, site_id, appt_datetime, FIRST_VALUE(visit_code) OVER w AS "first_value", NTH_VALUE(visit_code, 2) OVER w AS "second_value", NTH_VALUE(visit_code, 3) OVER w AS "third_value", FIRST_VALUE(appt_datetime) OVER w AS "first_date", NTH_VALUE(appt_datetime, 2) OVER w AS "second_date" FROM edc_appointment_appointment WHERE visit_code_sequence = 0 AND appt_status = \'New\' AND appt_datetime <= NOW() WINDOW w AS (PARTITION BY subject_identifier ORDER BY appt_datetime ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)) AS B WHERE NOT "second_value" IS NULL AND "third_value" IS NULL) as A ORDER BY site_id, from_now_days desc',
                "unattended_two_in_row_view",
                engine="django.db.backends.sqlite3",
            ),
            atomic=False,
        ),
    ]
