1 | |
---|
2 | |
---|
3 | CREATE OR REPLACE FUNCTION ws_resultados_de_encuesta_detalle(hab integer, form_hab character varying, elem character varying, preg integer) |
---|
4 | RETURNS SETOF record AS |
---|
5 | $BODY$ |
---|
6 | DECLARE |
---|
7 | tabla record; |
---|
8 | cond_preg character varying; |
---|
9 | BEGIN |
---|
10 | |
---|
11 | IF preg is not null THEN cond_preg := ' encuesta.pregunta = ' || preg; ELSE cond_preg := ' true '; END IF; |
---|
12 | |
---|
13 | RETURN QUERY |
---|
14 | select |
---|
15 | sed.encuesta_definicion, --0 |
---|
16 | sb.bloque, --1 |
---|
17 | sp.pregunta as pregunta_id, --2 |
---|
18 | sp.nombre as pregunta_texto, --3 |
---|
19 | scp.componente, --4 |
---|
20 | CASE WHEN (scp.tipo = 'A') THEN 'S' ELSE 'N' END AS es_libre, --5 |
---|
21 | CASE WHEN (scp.numero = 4) OR (scp.numero = 5) THEN 'S'ELSE 'N' END AS es_multiple,--6 |
---|
22 | sed.obligatoria, --7 |
---|
23 | sb.orden as bloque_orden, --8 |
---|
24 | sed.orden as pregunta_orden_bloque, --9 |
---|
25 | sb.orden || '_' || sed.orden as orden_en_encuesta, --10 |
---|
26 | |
---|
27 | CASE |
---|
28 | WHEN (scp.tipo = 'A') THEN null |
---|
29 | WHEN (scp.tipo = 'E' AND scp.componente = 'localidad' AND srd2.respuesta_valor <> '') THEN srd2.respuesta_valor::integer |
---|
30 | WHEN (scp.tipo = 'C') THEN sr.respuesta |
---|
31 | END AS respuesta_id, --11 |
---|
32 | spr.orden::character varying as repuesta_orden,--12 |
---|
33 | CASE |
---|
34 | WHEN (scp.tipo = 'A') THEN srd2.respuesta_valor |
---|
35 | WHEN (scp.tipo = 'E' and scp.componente = 'localidad' AND srd2.respuesta_valor <> '') THEN (SELECT nombre FROM mug_localidades WHERE localidad = srd2.respuesta_valor::integer ) |
---|
36 | WHEN (scp.tipo = 'C') THEN sr.valor_tabulado |
---|
37 | END AS respuesta_valor, --13 |
---|
38 | CASE |
---|
39 | WHEN (scp.tipo = 'A' OR scp.tipo = 'E' ) THEN count(srd2.respuesta_valor) |
---|
40 | ELSE count(srd.respuesta_codigo) |
---|
41 | END AS elegida_cantidad --14 |
---|
42 | |
---|
43 | FROM sge_habilitacion sh |
---|
44 | INNER JOIN sge_formulario_habilitado sfh ON (sh.habilitacion = sfh.habilitacion) |
---|
45 | LEFT JOIN sge_concepto sc on (sc.concepto = sfh.concepto) |
---|
46 | INNER JOIN sge_formulario_habilitado_detalle sfhd ON (sfhd.formulario_habilitado = sfh.formulario_habilitado) |
---|
47 | INNER JOIN sge_encuesta_atributo sea ON (sea.encuesta = sfhd.encuesta) |
---|
48 | LEFT JOIN sge_elemento se ON (se.elemento = sfhd.elemento) |
---|
49 | INNER JOIN sge_encuesta_definicion sed ON (sea.encuesta = sed.encuesta) |
---|
50 | INNER JOIN sge_bloque sb ON (sed.bloque = sb.bloque) |
---|
51 | INNER JOIN sge_pregunta sp ON (sp.pregunta = sed.pregunta and (sp.tabla_asociada = '' OR sp.tabla_asociada is NULL) ) |
---|
52 | INNER JOIN sge_componente_pregunta scp ON (scp.numero = sp.componente_numero) |
---|
53 | left JOIN sge_pregunta_respuesta spr on (spr.pregunta = sp.pregunta ) |
---|
54 | left JOIN sge_respuesta sr on (sr.respuesta = spr.respuesta) |
---|
55 | LEFT JOIN sge_respondido_formulario srf ON (srf.formulario_habilitado = sfh.formulario_habilitado) |
---|
56 | LEFT JOIN sge_respondido_encuesta sre ON (sre.respondido_formulario = srf.respondido_formulario AND sre.formulario_habilitado_detalle = sfhd.formulario_habilitado_detalle) |
---|
57 | LEFT JOIN sge_respondido_detalle srd ON (srd.respondido_encuesta = sre.respondido_encuesta |
---|
58 | and srd.encuesta_definicion = sed.encuesta_definicion and srd.respuesta_codigo = sr.respuesta) |
---|
59 | LEFT JOIN sge_respondido_detalle srd2 ON (srd2.respondido_encuesta = sre.respondido_encuesta |
---|
60 | and srd2.encuesta_definicion = sed.encuesta_definicion ) |
---|
61 | WHERE |
---|
62 | sh.habilitacion = hab AND |
---|
63 | sfh.formulario_habilitado_externo = form_hab AND |
---|
64 | se.elemento_externo = elem |
---|
65 | AND CASE WHEN preg is not null THEN sp.pregunta = preg ELSE true end |
---|
66 | |
---|
67 | GROUP BY sed.encuesta_definicion,sb.bloque, sp.pregunta, scp.componente, scp.tipo, scp.numero, sed.obligatoria, sed.orden, sr.respuesta,spr.orden, srd.respuesta_valor,srd2.respuesta_valor,sr.valor_tabulado; |
---|
68 | |
---|
69 | FOR tabla IN |
---|
70 | SELECT DISTINCT sp.tabla_asociada, sp.tabla_asociada_codigo, sp.tabla_asociada_descripcion, sp.tabla_asociada_orden_campo, sp.tabla_asociada_orden_tipo |
---|
71 | FROM sge_formulario_habilitado sfh |
---|
72 | INNER JOIN sge_formulario_habilitado_detalle sfhd ON (sfh.formulario_habilitado = sfhd.formulario_habilitado) |
---|
73 | INNER JOIN sge_encuesta_definicion sed ON (sfhd.encuesta = sed.encuesta) |
---|
74 | INNER JOIN sge_pregunta sp ON (sed.pregunta = sp.pregunta) |
---|
75 | WHERE sfh.habilitacion = hab AND sfh.formulario_habilitado_externo = form_hab AND sp.tabla_asociada != '' |
---|
76 | LOOP |
---|
77 | RETURN QUERY EXECUTE 'select |
---|
78 | encuesta.encuesta_definicion, --0 |
---|
79 | encuesta.bloque, --1 |
---|
80 | encuesta.pregunta as pregunta_id, --2 |
---|
81 | encuesta.pregunta_nombre as pregunta_texto, --3 |
---|
82 | encuesta.componente, --4 |
---|
83 | CASE WHEN (encuesta.tipo = ''A'') THEN ''S'' ELSE ''N'' END AS es_libre, --5 |
---|
84 | CASE WHEN (encuesta.numero = 4) OR (encuesta.numero = 5) THEN ''S'' ELSE ''N'' END AS es_multiple,--6 |
---|
85 | encuesta.obligatoria, --7 |
---|
86 | encuesta.bloque_orden, --8 |
---|
87 | encuesta.sed_orden as pregunta_orden_bloque, --9 |
---|
88 | encuesta.bloque_orden || ''_'' || encuesta.preg_orden as orden_en_encuesta, --10 |
---|
89 | encuesta.rta_codigo AS respuesta_id, --11 |
---|
90 | encuesta.rta_orden as repuesta_orden,--12 |
---|
91 | encuesta.rta_descripcion::character varying AS respuesta_valor--, --13 |
---|
92 | ,count(srd.respuesta_codigo) AS elegida_cantidad --14 |
---|
93 | FROM |
---|
94 | ( |
---|
95 | SELECT |
---|
96 | sed.encuesta_definicion, |
---|
97 | sb.bloque, |
---|
98 | sp.pregunta, |
---|
99 | scp.componente, |
---|
100 | scp.tipo, |
---|
101 | scp.numero, |
---|
102 | ta.' || tabla.tabla_asociada_codigo || ' , |
---|
103 | sea.encuesta, |
---|
104 | sed.orden as sed_orden, |
---|
105 | sed.obligatoria, |
---|
106 | sb.orden as bloque_orden, |
---|
107 | sed.orden as preg_orden, |
---|
108 | sp.tabla_asociada, |
---|
109 | sp.nombre as pregunta_nombre, |
---|
110 | ta.' || tabla.tabla_asociada_codigo || ' as rta_codigo, |
---|
111 | ta.' || CASE when (tabla.tabla_asociada_orden_campo = 'codigo') THEN tabla.tabla_asociada_codigo ELSE tabla.tabla_asociada_descripcion END || ' as rta_orden, |
---|
112 | ta.' || tabla.tabla_asociada_descripcion || ' as rta_descripcion |
---|
113 | FROM sge_encuesta_atributo sea INNER JOIN sge_encuesta_definicion sed ON (sea.encuesta = sed.encuesta) |
---|
114 | INNER JOIN sge_bloque sb ON (sed.bloque = sb.bloque) |
---|
115 | INNER JOIN sge_pregunta sp ON (sp.pregunta = sed.pregunta AND (sp.tabla_asociada != '''' OR sp.tabla_asociada is not NULL) ) |
---|
116 | INNER JOIN sge_componente_pregunta scp ON (scp.numero = sp.componente_numero), |
---|
117 | ' || tabla.tabla_asociada || ' ta |
---|
118 | ) as encuesta |
---|
119 | INNER JOIN sge_formulario_habilitado_detalle sfhd ON (encuesta.encuesta = sfhd.encuesta) |
---|
120 | INNER JOIN sge_formulario_habilitado sfh ON (sfh.formulario_habilitado = sfhd.formulario_habilitado) |
---|
121 | INNER JOIN sge_habilitacion sh ON (sh.habilitacion = sfh.habilitacion) |
---|
122 | LEFT JOIN sge_elemento se on (se.elemento = sfhd.elemento) |
---|
123 | LEFT JOIN sge_respondido_formulario srf ON (srf.formulario_habilitado = sfh.formulario_habilitado) |
---|
124 | LEFT JOIN sge_respondido_encuesta sre ON (sre.respondido_formulario = srf.respondido_formulario AND sre.formulario_habilitado_detalle = sfhd.formulario_habilitado_detalle) |
---|
125 | left join sge_respondido_detalle srd on (srd.respondido_encuesta = sre.respondido_encuesta and srd.encuesta_definicion = encuesta.encuesta_definicion and srd.respuesta_codigo = encuesta.rta_codigo) |
---|
126 | where sh.habilitacion = ' || hab || ' and |
---|
127 | encuesta.tabla_asociada = ''' || tabla.tabla_asociada || ''' and |
---|
128 | sfh.formulario_habilitado_externo = ''' || form_hab || ''' and |
---|
129 | se.elemento_externo = ''' || elem || ''' and '|| cond_preg ||' |
---|
130 | |
---|
131 | GROUP BY encuesta.encuesta_definicion, encuesta.bloque, encuesta.pregunta, encuesta.pregunta_nombre, encuesta.componente, encuesta.tipo, encuesta.numero, encuesta.obligatoria, |
---|
132 | encuesta.bloque_orden,encuesta.sed_orden,encuesta.preg_orden,encuesta.rta_codigo,encuesta.rta_orden, encuesta.rta_descripcion; |
---|
133 | ' ; |
---|
134 | END LOOP; |
---|
135 | |
---|
136 | RETURN; |
---|
137 | END; |
---|
138 | $BODY$ |
---|
139 | LANGUAGE plpgsql VOLATILE |
---|
140 | COST 100 |
---|
141 | ROWS 1000; |
---|
142 | |
---|