Advertisement
jzgeorge

funciones plc

Jul 26th, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION esq_ceac_planclase.f_concatenar_respuestas_docente_periodo(
  2.     p_idpersonal numeric,
  3.     p_idperiodo integer,
  4.     p_idmateria_unica integer)
  5.     RETURNS text
  6.     LANGUAGE 'plpgsql'
  7.  
  8.     COST 100
  9.     VOLATILE
  10. AS $BODY$
  11. DECLARE
  12.  evaluacionjson text;
  13. BEGIN
  14.  SELECT (('['::text || array_to_string(array_agg(regexp_replace(regexp_replace(tbl_plc_unidades_seguimiento.json_prom_preguntas, ']'::text, ''::text, 'g'::text), '\['::text, ''::text, 'g'::text)), ','::text)) || ']'::text) into evaluacionjson
  15.            FROM esq_ceac_planclase.tbl_plc_unidades_seguimiento
  16.           inner join esq_ceac_planclase.tbl_plc_planclase pc on pc.idregistro = tbl_plc_unidades_seguimiento.idplanclase
  17. WHERE pc.iddocente=p_idpersonal and pc.idperiodo = p_idperiodo and pc.idmateria = p_idmateria_unica;
  18.            return evaluacionjson;
  19. END;
  20. $BODY$;
  21.  
  22.  
  23. CREATE OR REPLACE FUNCTION esq_ceac_planclase.f_promedio_pregunta_docente(
  24.     p_idpersonal numeric,
  25.     p_idperiodo integer,
  26.     p_idpregunta numeric,
  27.     p_idmateria_unica integer)
  28.     RETURNS numeric
  29.     LANGUAGE 'plpgsql'
  30.  
  31.     COST 100
  32.     VOLATILE
  33. AS $BODY$
  34. DECLARE
  35. r record;
  36. v_promedio numeric;
  37. v_count integer;
  38. BEGIN
  39. v_count = 0;
  40. v_promedio = 0;
  41.  
  42. FOR r IN select (j.value ->> 'promedio')::numeric AS promedio
  43.  FROM json_array_elements((esq_ceac_planclase.f_concatenar_respuestas_docente_periodo(p_idpersonal,p_idperiodo,p_idmateria_unica))::json) j(value)
  44.  where (j.value ->> 'id_pregunta')::numeric = p_idpregunta
  45.   LOOP
  46.    v_count = v_count +1;
  47.     v_promedio = v_promedio + r.promedio;
  48.   END LOOP;
  49.  
  50.  if(v_count > 0 ) then
  51.   return v_promedio/v_count;
  52.  else
  53.   return 0;
  54.  end if;
  55.  
  56.          
  57. END;
  58. $BODY$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement