   ( SELECT name from darwin2.chronostratigraphy c2  WHERE level_ref = 57  AND c1.path like '%/' || c2.id || '/%' ) as subera,
   ( SELECT name from darwin2.chronostratigraphy c2  WHERE level_ref = 58  AND c1.path like '%/' || c2.id || '/%' ) as system,
   ( SELECT name from darwin2.chronostratigraphy c2  WHERE level_ref = 59  AND c1.path like '%/' || c2.id || '/%' ) as serie,
-  ( SELECT name from darwin2.chronostratigraphy c2  WHERE level_ref = 60  AND c1.path like '%/' || c2.id || '/%' ) as stage, 
+  ( SELECT name from darwin2.chronostratigraphy c2  WHERE level_ref = 60  AND c1.path like '%/' || c2.id || '/%' ) as stage,
   ( SELECT name from darwin2.chronostratigraphy c2  WHERE level_ref = 61  AND c1.path like '%/' || c2.id || '/%' ) as substage
   from darwin2.chronostratigraphy c1
-    
+
 );
 
 ALTER TABLE public.chronostratigraphy_abc ADD CONSTRAINT pk_chrono_abc PRIMARY KEY (id);
 
 CREATE TABLE public.accomp_mineral AS
 (
-
-
   SELECT
-    a.id,
-    f.id as flat_id,
+    r.id,
+    s.id as specimen_id,
     m.classification as classification,
     m.name as mineral_name
-   FROM 
-    darwin2.darwin_flat f
-    INNER JOIN darwin2.specimens_accompanying a ON f.spec_ref = a.specimen_ref
-    INNER JOIN darwin2.mineralogy m ON a.mineral_ref = m.id
-    WHERE accompanying_type ='mineral'
+   FROM
+    darwin2.specimens s
+    INNER JOIN darwin2.specimens_relationships r on s.id = r.specimen_ref
+    INNER JOIN darwin2.mineralogy m ON r.mineral_ref = m.id
+    WHERE unit_type ='mineralogy' and relationship_type ='combination'
 
 );
 

GRANT SELECT ON  public.taxon_identified TO d2viewer;
GRANT SELECT ON  public.mineral_identified TO d2viewer;
GRANT SELECT ON  public.identifier TO d2viewer;
GRANT SELECT ON  public.flat_properties TO d2viewer;
GRANT SELECT ON  public.lithostratigraphy_abc TO d2viewer;
GRANT SELECT ON  public.chronostratigraphy_abc TO d2viewer;
GRANT SELECT ON  public.accomp_mineral TO d2viewer;
GRANT SELECT ON  public.darwin_flat TO d2viewer;
GRANT SELECT ON  public.darwin_metadata TO d2viewer;
GRANT SELECT ON  public.parent_taxonomy TO d2viewer;

ANALYZE public.flat_abcd;
ANALYZE public.gtu_properties;

  p_col.title as collection_main_manager_title,

-- GTU

  CASE WHEN station_visible = true THEN gtu.elevation ELSE null::double precision END as gtu_altitude,
  CASE WHEN station_visible = true THEN gtu.elevation_accuracy ELSE null::double precision END as gtu_altitude_accuracy,
  CASE WHEN station_visible = true THEN gtu.latitude ELSE null::double precision END as gtu_latitude,
