text: SELECT
  d.docu_dk,
  d.docu_nr_mp nr_mp,
  nvl(d.docu_nr_externo,d.docu_nr_distribuicao) nr_externo,
  gs.nisi_ds_nivel_sigilo ds_sigilo,
  d.docu_dt_cadastro dt_cadastro,
  mt.mate_descricao materia,
  td.tpdc_descricao tp_doc,
  td.tpdc_sigla tp_doc_sigla,
  DOCU_TX_ETIQUETA etiqueta,
  oe2.orge_nm_orgao deleg_origem,
  oe3.orge_nm_orgao vara,
  fd.fsdc_ds_fase fase,
  ts.tpst_ds_tp_situacao situacao,
  cl.cldc_ds_classe classe,
  rtrim((
      SELECT (REVERSE ( SYS_CONNECT_BY_PATH ( REVERSE (trim(cls.cldc_ds_classe)), '|')))
      FROM mcpr_classe_docto_mp cls
      WHERE CONNECT_BY_ISLEAF = 1
      CONNECT BY PRIOR cls.cldc_cldc_dk_superior = cls.cldc_dk
      START WITH cls.cldc_dk = d.docu_cldc_dk),'|'
  ) AS classe_hierarquia,
  d.DOCU_ORGI_ORGA_DK_CARGA cdorgao_carga,
  d.DOCU_ORGI_ORGA_DK_RESPONSAVEL cdorgao_responsavel,
  orlacg.ORLW_ORGI_NM_ORGAO orgao_carga,
  orlares.ORLW_ORGI_NM_ORGAO orgao_responsavel,
  case when DECLINIO_DK is null then 0 else 1 end as declinio
  FROM mcpr_documento d
  LEFT JOIN mprj_materia mt ON d.docu_mate_dk = mt.mate_dk
  JOIN mcpr_tp_documento td ON d.docu_tpdc_dk = td.tpdc_dk
  LEFT JOIN mcpr_nivel_sigilo gs ON d.docu_nisi_dk = gs.nisi_dk
  LEFT JOIN mprj_orgao_ext oe2 ON oe2.orge_orga_dk = d.docu_orge_orga_dk_deleg_origem
  LEFT JOIN mprj_orgao_ext oe3 ON oe3.orge_orga_dk = d.docu_orge_orga_dk_vara
  LEFT JOIN mcpr_classe_docto_mp cl ON D.DOCU_CLDC_DK = CL.CLDC_DK
  LEFT JOIN mcpr_tp_situacao_documento ts ON d.docu_tpst_dk = ts.tpst_dk
  LEFT JOIN mcpr_fases_documento fd ON d.docu_fsdc_dk = fd.fsdc_dk
  LEFT JOIN orgi.ORGI_VW_ORGAO_LOCAL_ATUAL orlacg ON d.DOCU_ORGI_ORGA_DK_CARGA = orlacg.ORLW_DK
  LEFT JOIN orgi.ORGI_VW_ORGAO_LOCAL_ATUAL orlares ON d.DOCU_ORGI_ORGA_DK_RESPONSAVEL = orlares.ORLW_DK
  LEFT JOIN (
  SELECT docu_dk as DECLINIO_DK
  FROM mcpr.mcpr_documento
  JOIN mcpr.mcpr_vista v on docu_dk = vist_docu_dk
  LEFT JOIN mcpr.mcpr_andamento a ON v.vist_dk = a.pcao_vist_dk AND a.pcao_tpsa_dk = 2
  LEFT JOIN mcpr.mcpr_sub_andamento sad ON a.pcao_dk = sad.stao_pcao_dk
  WHERE stao_tppr_dk in (6265,6597,6598,6266)
  and vist_orgi_orga_dk = docu_orgi_orga_dk_responsavel
  group by docu_dk
  ) declinio on DOCU_DK = DECLINIO_DK
  WHERE DOCU_DK = :dk