select 'DROP VIEW IF EXISTS sce.vista_t'||cod_formulario||' CASCADE; 
		CREATE VIEW sce.vista_t'||cod_formulario||' as  
		SELECT '||array_to_string(agg1,', ')||'
		FROM sce.'||table_name||' t
		'||array_to_string(agg4,'
		')||' ;
		
		' as quy, agg1,agg5

		from(
			select cod_formulario,table_name,array_agg(alias_campo) agg1, array_agg(items) agg4, array_agg(nom_campo) agg5 FROM(
				select cod_formulario,table_name,data_type::text, nom_campo,
				case when itm is not null AND cod_tipo_campo in('select','radio' ) then
				'LEFT JOIN (
					SELECT CAST(valor_item as integer) as '||alias_campo||', nom_item as val_'||alias_campo||'
					FROM sce.item_campo 
					WHERE cod_campo='||cod_campo||'
				) c'||cod_campo||' USING('||alias_campo||')' 
				when cod_tipo_campo='selectmunicipio' then 
				'LEFT JOIN (
					SELECT id '||alias_campo||', nombre as val_'||alias_campo||',plan_zona_id as zon_'||alias_campo||'_id
					FROM plan_municipio 
				) c'||cod_campo||' USING('||alias_campo||')
				LEFT JOIN (
					SELECT id zon_'||alias_campo||'_id, nombre as zon_'||alias_campo||'_nombre
					FROM plan_zona 
				) cz'||cod_campo||' USING(zon_'||alias_campo||'_id)' 
				else NULL end as items,
				case when (itm is not null AND cod_tipo_campo in('select','radio') )  then
				'c'||cod_campo||'.val_'||alias_campo||' as '||alias_campo||',t.'||alias_campo||' as '||' val_'||alias_campo
				when (cod_tipo_campo='selectmunicipio')  then
				'c'||cod_campo||'.val_'||alias_campo||' as '||alias_campo||',t.'||alias_campo||' as '||' val_'||alias_campo||', '
				'cz'||cod_campo||'.zon_'||alias_campo||'_id ,zon_'||alias_campo||'_nombre'
				
				else 't.'||alias_campo end as alias_campo, orden_bloque,orden_campo
				from  sce.campo
				inner join sce.bloque_campo using(cod_campo)
				inner join sce.formulario_bloque using(cod_bloque)
				inner join information_schema.columns on(alias_campo=column_name and table_name='t'||cod_formulario)
				left join (select distinct cod_campo ,'s' as itm from sce.item_campo ) ic using(cod_campo)
				where cod_tipo_campo not like'matri%' 
				union 
				select  cod_formulario,'t'||cod_formulario,'character_varying','wf_caso_id',NULL,'t.wf_caso_id',0,0 from sce.formulario where tipo=0
				union 
				select  cod_formulario,'t'||cod_formulario,'oid','OID',NULL,'t.oid',0,0 from sce.formulario where tipo=0
				union 
				select  cod_formulario,'t'||cod_formulario,'bigint','OID_MAESTRO',NULL,'t.oid_maestro ',0,0 from sce.formulario
				inner join  information_schema.columns on (column_name = 'oid_maestro' and table_name = 't'||cod_formulario)
				union 
				select  cod_formulario,'t'||cod_formulario,'timestamp','Fecha de registro',NULL,'t.fecha_core_usuario_id_creacion as fec_creacion_reg',0,0 from sce.formulario
				
				order by cod_formulario, orden_bloque,orden_campo
			) z
			inner join  information_schema.tables tt using (table_name)
			group by cod_formulario,table_name
			order by cod_formulario
		) a
		where cod_formulario=18

--**************************************************************************************
select distinct nom_campo ,column_name as alias,
case when  column_name='oid' then 'oid'
	when column_name='wf_caso_id' then 'text' 
	when column_name='fec_creacion_reg' then 'text' 
        when cod_tipo_campo is null then 'selectitem'
else cod_tipo_campo 
end as tipo_campo,
case  when column_name='oid' then 'bigint'
	when column_name='wf_caso_id' then 'text' 
	when column_name='fec_creacion_reg' then 'timestamp' 
	when cod_tipo_campo is null then 'bigint'
	else data_type end as tipo_dato,
case  when column_name='oid' then 'OID' 
	when column_name='wf_caso_id' then 'wf_caso_id' 
	when column_name='fec_creacion_reg' then 'Fecha de registro' 
	when cod_tipo_campo is null then (select distinct nom_campo from sce.campo where alias_campo= SUBSTRING(column_name,5) )
	else nom_campo end as titulo
from information_schema.columns left join sce.campo on column_name=alias_campo 
where table_name = 'vista_t16';  

--************************************************************************************

select * from information_schema.tables where table_type='VIEW'