 	)
     AND s.altitude_min IS NOT NULL
     AND s.altitude_max IS NOT NULL
-GROUP BY t.cd_ref, period, 
-CASE WHEN p.active_life_stage=true THEN s.id_nomenclature_life_stage ELSE NULL END,
-id_altitude_range;
+GROUP BY t.cd_ref, period, CASE WHEN p.active_life_stage=true THEN s.id_nomenclature_life_stage 
+	ELSE NULL end)
+SELECT 
+cd_ref,
+period,
+id_nomenclature_life_stage,
+count_valid_data,
+extreme_altitude_min,
+my_alt_min[round(count_valid_data*(SELECT ((1-value::integer/100::float)/2)
+									FROM gn_profiles.t_parameters 
+									WHERE name='proportion_kept_data'))+1] as calculated_altitude_min,
+extreme_altitude_max,
+my_alt_max[round(count_valid_data*(SELECT ((1-value::integer/100::float)/2)
+									FROM gn_profiles.t_parameters 
+									WHERE name='proportion_kept_data'))+1] as calcultated_altitude_max
+from classified_data
+;
 
 
 CREATE VIEW gn_profiles.v_consistancy_data AS

	)
GROUP BY t.cd_ref;


CREATE MATERIALIZED VIEW gn_profiles.vm_cor_taxon_phenology AS 
SELECT DISTINCT
	t.cd_ref AS cd_ref,
	unnest(

			CASE 
				WHEN p.active_life_stage=true THEN s.id_nomenclature_life_stage 
				ELSE NULL 
			END,
			altitude_min,
			altitude_max)
		SELECT * 
		FROM myphenology mp, gn_profiles.vm_cor_taxon_phenology ctp
		WHERE ctp.cd_ref=mp.cd_ref
		AND mp.period=ctp.period
		AND mp.altitude_min >= ctp.calculated_altitude_min
		AND mp.altitude_max <= ctp.calcultated_altitude_max
		AND (ctp.id_nomenclature_life_stage=mp.id_nomenclature_life_stage 
		OR (ctp.id_nomenclature_life_stage IS NULL AND mp.id_nomenclature_life_stage IS NULL))
		)
	THEN
   	  RETURN true;
    ELSE
      RETURN false;
    END IF;
  END;
$function$
