Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE PROCEDURE [dbo].[spu_1_14_inf_hosp_desde_urg]
- @fechaini datetime, @fechafin datetime
- AS
- BEGIN
- SET NOCOUNT ON;
- SELECT * FROM( SELECT
- INGADM.PAC_NUMFICHA,
- INGADM.ING_CORREL,
- INGADM.ING_FECING,
- INGADM.ING_HORAING,
- PACIENTEADM.PAC_RUT,
- isnull(PACIENTEADM.PAC_NOMBRES,'')+' '+isnull(PACIENTEADM.PAC_PATERNO,'')+' ' +isnull(PACIENTEADM.PAC_MATERNO,'') AS NOMBRE,
- INGADM.SER_CODIGO,
- SERVICIO.SER_NOMBRE,
- INGADM.PTD_RUT_TRAT,
- PRESTADOR.PTD_NOMBRE,
- INGADM.ISA_CODIGO,
- ISAPRE.ISA_NOMBRE,
- ISNULL(DIAG_ALTA.CIE_CODIGO,'') AS CIE_CODIGO,
- ISNULL(CIE10.CIE_DESCRIP,'') AS CIE_DESCRIP,
- DIAG_ALTA.DIA_PRINCIPAL,
- ROW_NUMBER() OVER(PARTITION BY INGADM.PAC_NUMFICHA ORDER BY INGADM.PAC_NUMFICHA DESC, INGADM.ING_CORREL DESC) rn
- FROM INGRESO AS INGADM (nolock)
- LEFT JOIN FICHA AS FICHAADM (nolock) ON INGADM.PAC_NUMFICHA = FICHAADM.PAC_NUMFICHA
- LEFT JOIN PACIENTE AS PACIENTEADM (nolock) ON PACIENTEADM.PAC_CORREL = FICHAADM.PAC_CORREL
- LEFT JOIN DIAG_ALTA (nolock) ON DIAG_ALTA.PAC_NUMFICHA = INGADM.PAC_NUMFICHA AND DIAG_ALTA.ING_CORREL = INGADM.ING_CORREL
- LEFT JOIN CIE10 (nolock) ON CIE10.CIE_CODIGO = DIAG_ALTA.CIE_CODIGO
- LEFT JOIN
- ( SELECT *
- FROM SERVICIO (nolock)
- WHERE CHARINDEX('*'+SER_CODIGO+'*',(SELECT PAR_STR FROM PARAMETROS WHERE PAR_CODIGO = 'URG' ),1)= 0
- )SERVICIO ON INGADM.SER_CODIGO = SERVICIO.SER_CODIGO
- LEFT JOIN PRESTADOR (nolock) ON INGADM.PTD_RUT_TRAT = PRESTADOR.PTD_RUT
- LEFT JOIN ISAPRE (nolock) ON INGADM.ISA_CODIGO = ISAPRE.ISA_CODIGO
- WHERE INGADM.ING_TIPOATEN ='H'
- AND ((INGADM.ING_FECING >= @fechaini OR @fechaini IS NULL ) AND (INGADM.ING_FECING <= @fechafin OR @fechafin IS NULL ))
- AND ( EXISTS ( SELECT *
- FROM INGRESO AS INGURG (nolock)
- LEFT JOIN FICHA AS FICHAURG (nolock) ON INGURG.PAC_NUMFICHA = FICHAURG.PAC_NUMFICHA
- LEFT JOIN PACIENTE AS PACIENTEURG (nolock) ON PACIENTEURG.PAC_CORREL = FICHAURG.PAC_CORREL
- WHERE INGURG.SER_CODIGO IN ( SELECT SER_CODIGO FROM SERVICIO (nolock) WHERE CHARINDEX('*'+SER_CODIGO+'*',(SELECT PAR_STR FROM PARAMETROS WHERE PAR_CODIGO = 'URG' ),1)<> 0)
- AND PACIENTEURG.PAC_RUT =PACIENTEADM.PAC_RUT
- AND ( DATEDIFF(HOUR, IsNull(INGURG.ING_HORAALTA,0), INGADM.ING_HORAING)<=2
- AND DATEDIFF(HOUR, IsNull(INGURG.ING_HORAALTA,0), INGADM.ING_HORAING) >= 0 ))
- OR INGADM.PAC_NUMFICHA_URG IS NOT NULL)
- ) x
- WHERE DIA_PRINCIPAL = 'S' OR (rn =1 AND (DIA_PRINCIPAL='N' OR DIA_PRINCIPAL IS NULL))
- ORDER BY pac_numficha, ing_correl, ing_fecing, ing_horaing, pac_rut, nombre, ser_codigo, ser_nombre, ptd_rut_trat,ptd_nombre,isa_codigo,isa_nombre
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement