Skip to content

Instantly share code, notes, and snippets.

@hatus
Created October 15, 2025 17:32
Show Gist options
  • Select an option

  • Save hatus/a6876940f6a373361a958b58b907bdee to your computer and use it in GitHub Desktop.

Select an option

Save hatus/a6876940f6a373361a958b58b907bdee to your computer and use it in GitHub Desktop.

Revisions

  1. hatus renamed this gist Oct 15, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. hatus created this gist Oct 15, 2025.
    169 changes: 169 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,169 @@
    SELECT
    w.nr_atendimento,
    w.nm_pessoa_fisica,
    w.ie_triagem,
    w.ie_consulta,
    w.ie_medicacao,
    w.ie_laboratorio,
    w.ie_imagem,
    case
    when ((w.ie_medicacao = 'Não Solicitado' or w.ie_medicacao = 'Medicação Liberada') and
    (w.ie_laboratorio = 'Não Solicitado' or w.ie_laboratorio = 'Laudo Liberado') and
    (w.ie_imagem = 'Não Solicitado' or w.ie_imagem = 'Laudo Liberado') and
    w.ie_consulta = 'Atendido' and w.ie_desfecho is null) then 'Dirija-se ao Consultório'
    when w.ie_desfecho = 'A' then 'Alta do Pronto Socorro'
    when w.ie_desfecho = 'I' then 'Alta para Internação'
    when w.ie_desfecho = 'T' then 'Alta para transferência'
    else 'Pendente'
    end ie_alta
    FROM
    (select
    ap.nr_atendimento,
    nvl(pf.nm_social, pf.nm_pessoa_fisica) nm_pessoa_fisica,
    ap.dt_alta,
    aa.ie_desfecho,
    /* FASE TRIAGEM */
    case
    when (tpa.dt_fim_triagem is null and local_triagem.ds_local_curto is not null) then local_triagem.ds_local_curto || ' Chamando'
    when (tpa.dt_fim_triagem is not null and tpa.dt_fim_triagem is not null) then 'Triagem Realizada'
    else 'Aguardando Triagem'
    end ie_triagem,
    /* FASE MÉDICO */
    case
    when (ap.dt_atend_medico is null and local_medico.ds_local_curto is not null) then 'Médico Chamando'|| ' (' || local_medico.ds_local_curto || ')'
    when (ap.dt_atend_medico is not null and ap.dt_fim_consulta is null) then 'Consulta Iniciada'
    when (ap.dt_atend_medico is not null and ap.dt_fim_consulta is not null) then 'Atendido'
    else 'Aguardando Consulta Médica'
    end ie_consulta,
    /* FASE MEDICAÇÃO */
    case when
    (select 1 FROM
    tasy.prescr_medica pm, tasy.prescr_material pmat WHERE
    pm.nr_atendimento = ap.nr_atendimento and
    pmat.nr_prescricao = pm.nr_prescricao fetch first 1 rows only) is null then 'Não Solicitado'
    else
    (SELECT case when max(pmat.dt_baixa) is not null then 'Medicação Liberada' else 'Realizar Medicação' end ie_medicacao
    FROM tasy.prescr_medica pm, tasy.prescr_material pmat WHERE
    pm.nr_atendimento = ap.nr_atendimento and
    pmat.nr_prescricao = pm.nr_prescricao)
    end ie_medicacao,
    /* FASE LABORATÓRIO */
    case when
    (SELECT 1 FROM dual where not exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null)) is not null then 'Não Solicitado'
    when
    (SELECT 1 FROM dual where exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend < 20)) is not null then 'Aguardando Coleta Laboratório'
    when
    (SELECT 1 FROM dual where exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend >= 20)
    and not exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend < 20)
    and not exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend = 35)
    ) is not null then 'Coleta Realizada / Aguardando Laudo'
    when
    (SELECT 1 FROM dual where exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend = 35)
    and exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend < 35)
    ) is not null then 'Parcial Liberado'
    when
    (SELECT 1 FROM dual where exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend = 35)
    and not exists( select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp WHERE
    pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is not null and pp.ie_status_atend < 35)
    ) is not null then 'Laudo Liberado'
    end ie_laboratorio,
    /* FASE EXAME DE IMAGEM */
    case when
    (SELECT 1 FROM dual where not exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and p.cd_tipo_procedimento in (2,3,4,15,34)
    and pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and pp.nr_seq_exame is null)) is not null then 'Não Solicitado'
    when
    (SELECT 1 FROM dual where exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and p.cd_tipo_procedimento in (2,3,4,15,34)
    and pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is null and pp.ie_status_execucao < 20)) is not null then 'Aguardando Realização do Exame'
    when
    (SELECT 1 FROM dual where exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and p.cd_tipo_procedimento in (2,3,4,15)--,34)
    and pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is null and pp.ie_status_execucao = 20)
    and not exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and p.cd_tipo_procedimento in (2,3,4,15,34)
    and pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is null and pp.ie_status_execucao < 20)
    and not exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and p.cd_tipo_procedimento in (2,3,4,15,34)
    and pm.nr_atendimento = ap.nr_atendimento and pp.nr_prescricao = pm.nr_prescricao and
    pp.nr_seq_exame is null and pp.ie_status_execucao > 20)
    ) is not null then 'Exame Realizado / Aguardando Laudo'
    when
    (SELECT 1 FROM dual where exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and pm.nr_atendimento = ap.nr_atendimento and
    pp.nr_prescricao = pm.nr_prescricao and pp.nr_seq_exame is null and
    ((pp.ie_status_execucao = 40 and p.cd_tipo_procedimento in (2,3,4,15)) or (pp.ie_status_execucao = 20 and p.cd_tipo_procedimento in (34))))
    and exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and pm.nr_atendimento = ap.nr_atendimento and
    pp.nr_prescricao = pm.nr_prescricao and pp.nr_seq_exame is null and
    ((pp.ie_status_execucao < 40 and p.cd_tipo_procedimento in (2,3,4,15)) or (pp.ie_status_execucao < 20 and p.cd_tipo_procedimento in (34))))
    ) is not null then 'Parcial Liberado'
    when
    (SELECT 1 FROM dual where exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and pm.nr_atendimento = ap.nr_atendimento and
    pp.nr_prescricao = pm.nr_prescricao and pp.nr_seq_exame is null and
    ((pp.ie_status_execucao = 40 and p.cd_tipo_procedimento in (2,3,4,15)) or (pp.ie_status_execucao = 20 and p.cd_tipo_procedimento in (34))))
    and not exists (select 1 from tasy.prescr_medica pm, tasy.prescr_procedimento pp, tasy.procedimento p
    WHERE p.cd_procedimento = pp.cd_procedimento and p.ie_origem_proced = pp.ie_origem_proced and pm.nr_atendimento = ap.nr_atendimento and
    pp.nr_prescricao = pm.nr_prescricao and pp.nr_seq_exame is null and
    ((pp.ie_status_execucao < 40 and p.cd_tipo_procedimento in (2,3,4,15)) or (pp.ie_status_execucao < 20 and p.cd_tipo_procedimento in (34))))
    ) is not null then 'Laudo Liberado'
    end ie_imagem
    from
    tasy.atendimento_paciente ap,
    tasy.pessoa_fisica pf,
    TASY.TRIAGEM_PRONTO_ATEND tpa,
    tasy.atendimento_alta aa,
    (select
    mls.ds_local_curto,
    ap.nr_atendimento
    from
    TASY.fila_visualizacao_senha fvs,
    tasy.maquina_local_senha mls,
    tasy.atendimento_paciente ap
    where
    fvs.dt_atualizacao > sysdate-1 and
    mls.nr_sequencia = nr_seq_local_chamada and
    ap.nr_seq_pac_senha_fila = fvs.nr_seq_pac_senha_fila and
    mls.nr_seq_grupo_local_senha = 6) local_triagem,
    (select
    mls.ds_local_curto,
    ap.nr_atendimento
    from
    TASY.fila_visualizacao_senha fvs,
    tasy.maquina_local_senha mls,
    tasy.atendimento_paciente ap
    where
    fvs.dt_atualizacao > sysdate-1 and
    mls.nr_sequencia = nr_seq_local_chamada and
    ap.nr_seq_pac_senha_fila = fvs.nr_seq_pac_senha_fila and
    mls.nr_seq_grupo_local_senha = 7) local_medico
    where
    ap.cd_pessoa_fisica = pf.cd_pessoa_fisica and
    ap.nr_atendimento = tpa.nr_atendimento and
    ap.ie_tipo_atendimento = 3 and
    ap.nr_atendimento = local_triagem.nr_atendimento(+) and
    ap.nr_atendimento = local_medico.nr_atendimento(+) and
    ap.nr_atendimento = aa.nr_atendimento(+)) W
    WHERE
    w.nr_atendimento = :nr_atendimento