                                   (
                                       (
                                           max(
-                                                  length(
-                                                          metadata.item_name
-                                                  )
+                                              length(
+                                                  metadata.item_name
+                                              )
                                           )
                                       ) > 12
                                       OR (
                                              max(
-                                                     CASE WHEN
-                                                         metadata.item_name
-                                                         ~
-                                                         $reg$^[0-9].+$$reg$ THEN length(
-                                                                 metadata.item_name) END)
+                                                 CASE WHEN
+                                                     metadata.item_name
+                                                     ~
+                                                     $reg$^[0-9].+$$reg$ THEN length(
+                                                         metadata.item_name) END)
                                          ) > 0
                                   ) AS use_item_oid
                               FROM
                                   dm.metadata_crf_ig_item AS metadata
                               GROUP BY
                                   study_name
-        ),
-                crf_nulls AS
-            (
+        ), crf_nulls AS
+        (
+            SELECT
+                trim(
+                    BOTH
+                    ','
+                    FROM
+                    (array_to_string(
+                        array_agg(
+                            quote_literal(
+                                trim(
+                                    BOTH
+                                    ','
+                                    FROM
+                                    (
+                                        sub.crf_null_values
+                                    )
+                                )
+                            )
+                        ),
+                        $$,$$
+                    ))
+                ) AS crf_null_values,
+                study_name,
+                item_group_oid
+            FROM
+                (
                     SELECT
-                        trim(
-                                BOTH
-                                ','
-                                FROM
-                                (array_to_string(
-                                        array_agg(
-                                                quote_literal(
-                                                        trim(
-                                                                BOTH
-                                                                ','
-                                                                FROM
-                                                                (
-                                                                    sub.crf_null_values
-                                                                )
-                                                        )
-                                                )
-                                        ),
-                                        $$,$$
-                                ))
-                        ) AS crf_null_values,
-                        study_name,
-                        item_group_oid
+                        metadata.study_name,
+                        metadata.item_group_oid,
+                        metadata.crf_null_values
                     FROM
-                        (
-                            SELECT
-                                metadata.study_name,
-                                metadata.item_group_oid,
-                                metadata.crf_null_values
-                            FROM
-                                dm.metadata_event_crf_ig AS metadata
-                            WHERE
-                                metadata.crf_null_values != $$$$
-                        ) AS sub
-                    GROUP BY
-                        study_name,
-                        item_group_oid
-            )
+                        dm.metadata_event_crf_ig AS metadata
+                    WHERE
+                        metadata.crf_null_values != $$$$
+                ) AS sub
+            GROUP BY
+                study_name,
+                item_group_oid
+        )
 
-        SELECT
-            format(
-                    $$CREATE %1$s VIEW %2$I.%3$I AS SELECT study_name,
+        SELECT format(
+                   $$CREATE %1$s VIEW %2$I.%3$I AS SELECT study_name,
                      site_oid, site_name, subject_id, event_oid, event_name,
                      event_order, event_repeat, crf_parent_name, crf_version,
                      crf_status, item_group_oid, item_group_repeat, %4$s %5$s $$,
-                    (
-                        CASE
-                        WHEN alias_views
-                        THEN $$$$
-                        ELSE $$MATERIALIZED$$
-                        END
-                    ),
-                    dm_clean_name_string(
-                            ddl.study_name
-                    ),
-                    (
-                        CASE
-                        WHEN alias_views THEN concat(
-                                $$av_$$,
-                                ddl.item_group_oid
-                        )
-                        ELSE ddl.item_group_oid
-                        END
-                    ),
-                    array_to_string(
-                            array_agg(
-                                    (
-                                        CASE
-                                        WHEN alias_views
-                                        THEN ddl.item_ddl_av
-                                        ELSE ddl.item_ddl
-                                        END
-                                    )
-                            ),
-                            $$,$$
-                    ),
-                    (
-                        CASE
-                        WHEN alias_views
-                        THEN ddl.ig_ddl_av
-                        ELSE ddl.ig_ddl
-                        END
-                    )
-            ) AS create_statement
+                   (
+                       CASE
+                       WHEN alias_views
+                       THEN $$$$
+                       ELSE $$MATERIALIZED$$
+                       END
+                   ),
+                   dm_clean_name_string(
+                       ddl.study_name
+                   ),
+                   (
+                       CASE
+                       WHEN alias_views THEN concat(
+                           $$av_$$,
+                           ddl.item_group_oid
+                       )
+                       ELSE ddl.item_group_oid
+                       END
+                   ),
+                   array_to_string(
+                       array_agg(
+                           (
+                               CASE
+                               WHEN alias_views
+                               THEN ddl.item_ddl_av
+                               ELSE ddl.item_ddl
+                               END
+                           )
+                       ),
+                       $$,$$
+                   ),
+                   (
+                       CASE
+                       WHEN alias_views
+                       THEN ddl.ig_ddl_av
+                       ELSE ddl.ig_ddl
+                       END
+                   )
+               ) AS create_statement
         FROM
             (
                 SELECT
                     format(
-                            $$ %1$s %2$s $$
-                            ,
-                            format
-                            (
-                                    $$ max(case when item_oid=%1$L
+                        $$ %1$s %2$s $$
+                        ,
+                        format
+                        (
+                            $$ max(case when item_oid=%1$L
                                       then (case when item_value = '' then null
                                       when item_value IN (%2$s)
                                       then null else cast(item_value as %3$s
                                       ) end) else null end) as %4$I $$,
-                                    met.item_oid,
-                                    CASE
-                                    WHEN met.crf_null_values IS NULL
-                                    THEN $$''$$
-                                    ELSE met.crf_null_values
-                                    END,
-                                    CASE
-                                    WHEN
-                                        item_data_type
-                                        IN
-                                        ($$ST$$, $$PDATE$$, $$FILE$$)
-                                    THEN $$text$$
-                                    WHEN
-                                        item_data_type
-                                        IN
-                                        ($$INT$$, $$REAL$$)
-                                    THEN $$numeric$$
-                                    ELSE item_data_type
-                                    END,
-                                    met.item_name_hint
-                            )
-                            ,
-                            (
-                                CASE
-                                WHEN met.item_response_set_label IS NULL
-                                THEN NULL
-                                ELSE format(
-                                        $$ , max(case when item_oid=%1$L
+                            met.item_oid,
+                            CASE
+                            WHEN met.crf_null_values IS NULL
+                            THEN $$''$$
+                            ELSE met.crf_null_values
+                            END,
+                            CASE
+                            WHEN
+                                item_data_type
+                                IN
+                                ($$ST$$, $$PDATE$$, $$FILE$$)
+                            THEN $$text$$
+                            WHEN
+                                item_data_type
+                                IN
+                                ($$INT$$, $$REAL$$)
+                            THEN $$numeric$$
+                            ELSE item_data_type
+                            END,
+                            met.item_name_hint
+                        )
+                        ,
+                        (
+                            CASE
+                            WHEN met.item_response_set_label IS NULL
+                            THEN NULL
+                            ELSE format(
+                                $$ , max(case when item_oid=%1$L
                                           then (case when item_value = ''
                                           then null when item_value IN (%2$s)
                                           then null else option_text end)
                                           else null end) as %3$s_label$$,
-                                        met.item_oid,
-                                        CASE
-                                        WHEN met.crf_null_values IS NULL
-                                        THEN $$''$$
-                                        ELSE met.crf_null_values
-                                        END,
-                                        met.item_name_hint
-                                )
-                                END
+                                met.item_oid,
+                                CASE
+                                WHEN met.crf_null_values IS NULL
+                                THEN $$''$$
+                                ELSE met.crf_null_values
+                                END,
+                                met.item_name_hint
                             )
+                            END
+                        )
                     ) AS item_ddl,
                     format(
-                            $$ FROM %1$I.clinicaldata WHERE
+                        $$ FROM %1$I.clinicaldata WHERE
                                       item_group_oid=%2$L GROUP BY study_name,
                                       site_oid, site_name, subject_id, event_oid,
                                       event_name, event_order, event_repeat,
                                       crf_parent_name, crf_version, crf_status,
                                       item_group_oid, item_group_repeat;$$,
-                            dm_clean_name_string(
-                                    met.study_name
-                            ),
-                            upper(
-                                    met.item_group_oid
-                            )
+                        dm_clean_name_string(
+                            met.study_name
+                        ),
+                        upper(
+                            met.item_group_oid
+                        )
                     ) AS ig_ddl,
                     item_group_oid,
                     study_name,
                     format(
-                            $$ %1$s %2$s $$
-                            ,
-                            format(
-                                    $$ %1$s AS %2$s $$,
-                                    met.item_name_hint,
-                                    met.item_name
+                        $$ %1$s %2$s $$
+                        ,
+                        format(
+                            $$ %1$s AS %2$s $$,
+                            met.item_name_hint,
+                            met.item_name
+                        )
+                        ,
+                        (
+                            CASE
+                            WHEN met.item_response_set_label IS NULL
+                            THEN NULL
+                            ELSE format(
+                                $$ , %1$s_label AS %2$s_label $$,
+                                met.item_name_hint,
+                                met.item_name
                             )
-                            ,
-                            (
-                                CASE
-                                WHEN met.item_response_set_label IS NULL
-                                THEN NULL
-                                ELSE format(
-                                        $$ , %1$s_label AS %2$s_label $$,
-                                        met.item_name_hint,
-                                        met.item_name
-                                )
-                                END
-                            )
+                            END
+                        )
                     ) AS item_ddl_av,
                     format(
-                            $$ FROM %1$I.%2$I;$$,
-                            dm_clean_name_string(
-                                    met.study_name
-                            ),
-                            met.item_group_oid
+                        $$ FROM %1$I.%2$I;$$,
+                        dm_clean_name_string(
+                            met.study_name
+                        ),
+                        met.item_group_oid
                     ) AS ig_ddl_av,
                     item_form_order
                 FROM

                                    (
                                        CASE
                                        WHEN length(
                                                     filter_itemgroup_oid
                                             ) > 0
                                        THEN dm_meta.item_group_oid =
                                             filter_itemgroup_oid
                                        ELSE TRUE END
                                    )
                            ) AS namecheck
                        GROUP BY
                            study_name,

                        SELECT
                            study_name,
                            lower(
                                item_group_oid
                            ) AS item_group_oid,
                            item_oid,
                            lower(
                                item_name
                            ) AS item_name,
                            lower(
                                CASE
                                WHEN length(
                                         item_name_hint
                                     ) > 57
                                THEN substr(
                                    item_name_hint,
                                    1,
                                    57
                                )
                                ELSE item_name_hint
                                END
                            ) AS item_name_hint,
                            item_data_type,
                            max(
                                item_form_order
                            ) AS item_form_order,
                            max(
                                item_response_set_label
                            ) AS item_response_set_label,
                            crf_null_values
                        FROM
