 
 -- TODO: insert procedure
 
-select * from fhir.expanded_with_dt_resource_elements
-where path[1]='Patient' and path[2]='name'
+--}}}
+--{{{
+
+select * from fhir.expanded_resource_elements
+where path[1]='Patient' and path[2]='address'
 order by path
 --}}}


-- expand polimorphic types
CREATE
VIEW fhir.expanded_resource_elements as (
  SELECT
    array_pop(path) || ARRAY[column_name(array_last(path), type)] as path,
    type,

      CASE WHEN array_length(t.path,1) = 1
        THEN e.max
        ELSE t.max
      END AS max,
      CASE WHEN array_length(t.path,1) = 1
        THEN e.type
        ELSE t.type
      END AS type
    FROM fhir.polimorphic_expanded_resource_elements e
    JOIN fhir.datatype_unified_elements t
    ON t.path[1] = e.type
);
