Skip to content

Commit

Permalink
😸 Relax measure_time requirement and only check in current schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
psbrandt committed Jan 3, 2020
1 parent 1f483e5 commit c821fc8
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ $$
DECLARE
obs_time_exists boolean := false;
obs_time_col_string varchar := '';
measurement_time_exists boolean := false;
measurement_col_string varchar := '';
BEGIN
SELECT EXISTS(SELECT table_name
FROM information_schema.columns
WHERE table_name = 'observation'
AND column_name = 'observation_time')
AND column_name = 'observation_time'
AND table_schema = current_schema())
into obs_time_exists;

CASE WHEN obs_time_exists = TRUE
Expand All @@ -18,13 +21,27 @@ $$
obs_time_col_string = 'observation.observation_datetime::timestamp::time::varchar(10)';
END CASE;

SELECT EXISTS(SELECT table_name
FROM information_schema.columns
WHERE table_name = 'measurement'
AND column_name = 'measurement_time'
AND table_schema = current_schema())
into measurement_time_exists;

CASE WHEN measurement_time_exists = TRUE
THEN
measurement_col_string = 'measurement_time';
ELSE
measurement_col_string = 'measurement.measurement_datetime::timestamp::time::varchar(10)';
END CASE;

EXECUTE $sql$
CREATE view f_observation_view AS
SELECT measurement.measurement_id AS observation_id,
measurement.person_id,
measurement.measurement_concept_id AS observation_concept_id,
measurement.measurement_date AS observation_date,
measurement.measurement_time AS observation_time,
$sql$ || measurement_col_string || $sql$ AS observation_time,
measurement.measurement_type_concept_id AS observation_type_concept_id,
measurement.operator_concept_id AS observation_operator_concept_id,
measurement.value_as_number,
Expand Down Expand Up @@ -74,4 +91,4 @@ $$
((c.invalid_reason) :: text <> 'D' :: text)))))
$sql$;
END create_observation_view
$$;
$$;

0 comments on commit c821fc8

Please sign in to comment.