Last active
January 29, 2018 15:25
-
-
Save MarioCares/a19844529e5f5627914e4df4955d523f to your computer and use it in GitHub Desktop.
Cruce Validados Rayen - Percapita
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- PRIMERO, ELIMINAR POSIBLES DUPLICADOS | |
select * | |
into dbo.temporal_filtro | |
from USP_USUARIO_APS | |
where FECHA_INSCRIPCION in ( SELECT MAX(FECHA_INSCRIPCION) from USP_USUARIO_APS group by RUT); | |
-- EXTRAER DATOS NECESARIOS DESDE PERCAPITA. CAMBIAR CÓDIGO CENTRO FONASA POR RAYEN | |
select concat(run, dv) as run, | |
case cod_centro when 102300 then 225 when 102301 then 227 when 102302 then 226 | |
when 102306 then 2957 when 102412 then 226 when 102413 then 226 when 102701 then 3003 end as cod_centro, | |
nombre_centro as pertenece_a, nombre_centro_procedencia, nombre_comuna_procedencia, nombre_centro_destino, nombre_comuna_destino, | |
aceptado_rechazado, motivo from registro; | |
-- IMPORTAR LOS DATOS EN UNA NUEVA TABLA ... | |
-- AHORA, GENERAR QUERY | |
SELECT uua.rut, uua.NOMBRES, uua.APELLIDO_PATERNO, uua.APELLIDO_MATERNO, uua.FECHA_DE_NACIMIENTO, hl7_01.NOMBRE as sexo, nac.NOMBRE as nacionalidad, | |
uua.EMAIL, uua.DIRECCION as direccion1, | |
concat(uua.CASA,' ', uua.BLOCK, ' ', uua.DEPARTAMENTO, ' ', uua.VILLA_O_POBLACION) as direccion2, | |
concat(fam.VILLA_POBLACION, ' ', fam.CASA, ' ', fam.BLOCK, ' ', fam.DEPTO, ' ', fam.SITIO, ' ', fam.CALLE) as direccion3, | |
concat(uua.TELEFONO1, '/' , uua.TELEFONO2, '/', fam.TELEFONO1, '/', fam.TELEFONO2) as telefonos, | |
sec.NOMBRE as sector, ccp.NOMBRE as prevision, ocp.NOMBRE as ocupacion, nod.RAZON_SOCIAL as nodo, | |
uua.NUMERO_IDENTIFICACION, | |
DATEDIFF(YY, uua.FECHA_DE_NACIMIENTO, GETDATE())-CASE WHEN DATEADD(YY, DATEDIFF(YY, uua.FECHA_DE_NACIMIENTO, GETDATE()), uua.FECHA_DE_NACIMIENTO) | |
> GETDATE() THEN 1 | |
ELSE 0 END AS [agno], p.* | |
FROM temporal_filtro as uua | |
inner join CNP_CONVENIO_PREVISIONAL as ccp on ccp.id = uua.CNP_ID | |
inner join HL7_0001_U as hl7_01 on hl7_01.ID = uua.HL7_0001_U_ID | |
left join OCP_OCUPACION as ocp on ocp.id = uua.OCP_ID | |
left join NAC_NACIONALIDAD as nac on nac.id = uua.NAC_ID | |
inner join NOD_NODO as nod on nod.ID = uua.NOD_ID | |
left join FAM_FAMILIA as fam on fam.id = uua.FAM_ID | |
left join SEC_SECTOR as sec on sec.id = fam.SEC_ID | |
left join percapita as p on p.run Collate database_default = uua.RUT | |
-- FILTRAR POR NODO SEGÚN RAYEN Y LISTO :D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment