Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
1
Indexable
Never
CREATE OR REPLACE FUNCTION mdm.codelco_average_time_complete_cataloging(organization integer DEFAULT NULL::integer, creation_user uuid DEFAULT NULL::uuid)
 RETURNS SETOF mdm.codelco_step_log
 LANGUAGE plpgsql
AS $function$
	declare query text := '';
begin
	IF organization IS NOT NULL then
		query := 'select CAST((DATE_PART(''day'', tb.integrated_action_date::timestamp - tb.creation_action_date::timestamp) * 24 +  DATE_PART(''hour'', tb.integrated_action_date::timestamp - tb.creation_action_date::timestamp)) as int4) as id,  workflow_id, material_creation_id, action_id ,action_date ,end_action ,user_id , before_step , actual_step ,rejection_reason_id ,"comment" from (
			select *,(select action_date  from mdm.codelco_step_log cs where cs.material_creation_id = csl.material_creation_id  and cs.action_id = 25 limit 1 ) as "creation_action_date", csl.action_date as "integrated_action_date"  from mdm.codelco_step_log csl 
			inner join mdm.codelco_material_creation cmc on cmc.id = csl.material_creation_id
			where  action_id in  (28) and organization_division_id =' ||  organization || ' ) as tb 
			where tb.creation_action_date is not null;';
	ELSE
		query := 'select CAST((DATE_PART(''day'', tb.integrated_action_date::timestamp - tb.creation_action_date::timestamp) * 24 +  DATE_PART(''hour'', tb.integrated_action_date::timestamp - tb.creation_action_date::timestamp)) as int4) as id,  workflow_id, material_creation_id, action_id ,action_date ,end_action ,user_id , before_step , actual_step ,rejection_reason_id ,"comment" from (
			select *,(select action_date  from mdm.codelco_step_log cs where cs.material_creation_id = csl.material_creation_id  and cs.action_id = 25 limit 1 ) as "creation_action_date", csl.action_date as "integrated_action_date"  from mdm.codelco_step_log csl 
			inner join mdm.codelco_material_creation cmc on cmc.id = csl.material_creation_id
			where  action_id in  (28)) as tb 
			where tb.creation_action_date is not null;';
	END IF; 
RETURN QUERY EXECUTE query;
END;
$function$