Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH setores_resposta AS (
  2.     SELECT
  3.             ROW_NUMBER() over (partition by th001.id_notificacao_solicitacao ORDER BY th001.id_notificacao_solicitacao_status) as linha
  4.             ,setores.setor
  5.             ,max(th001.dt_alteracao) as dt_resposta
  6.             ,th001.id_notificacao_solicitacao_status
  7.             ,th001.id_notificacao_solicitacao as id_solicitacao
  8.     FROM th001_notificacao_solicitacao_status AS th001
  9.     JOIN pentaho.tb_setores AS setores
  10.             ON 1=1
  11.             AND setores.emails ILIKE '%'||fn_retorna_email(th001.de_resposta)||'%'
  12.     WHERE --th001.id_notificacao_solicitacao = 1186
  13.      th001.id_status_notificacao_solicitacao_atual = 35
  14.    
  15.     GROUP BY 2,4,5
  16.     order by 5,1
  17.  
  18. ), data_encaminhamento as (
  19.         SELECT th001.id_notificacao_solicitacao as id_solicitacao
  20.                   ,min(th001.dt_alteracao) as dt_encaminhamento
  21.         FROM th001_notificacao_solicitacao_status AS th001
  22.         WHERE --th001.id_notificacao_solicitacao = 1186
  23.              th001.id_status_notificacao_solicitacao_atual = 41
  24.         GROUP BY 1
  25. )
  26.  
  27.  
  28. SELECT distinct
  29.  
  30.              sr.id_solicitacao
  31.             ,(select setor from setores_resposta where linha = 1 and id_solicitacao = tb017.id_notificacao_solicitacao) as setor_1
  32.             ,(select setor from setores_resposta where linha = 2 and id_solicitacao = tb017.id_notificacao_solicitacao) as setor_2
  33.             ,(select setor from setores_resposta where linha = 3 and id_solicitacao = tb017.id_notificacao_solicitacao) as setor_3
  34.             ,(select setor from setores_resposta where linha = 4 and id_solicitacao = tb017.id_notificacao_solicitacao) as setor_4
  35.             ,(select setor from setores_resposta where linha = 5 and id_solicitacao = tb017.id_notificacao_solicitacao) as setor_5
  36.           ,dte.dt_encaminhamento as dt_encaminhamento
  37.             ,(select dt_resposta from setores_resposta where linha = 1 and id_solicitacao = tb017.id_notificacao_solicitacao) as dt_resposta_1
  38.             ,(select dt_resposta from setores_resposta where linha = 2 and id_solicitacao = tb017.id_notificacao_solicitacao) as dt_resposta_2
  39.             ,(select dt_resposta from setores_resposta where linha = 3 and id_solicitacao = tb017.id_notificacao_solicitacao) as dt_resposta_3
  40.             ,(select dt_resposta from setores_resposta where linha = 4 and id_solicitacao = tb017.id_notificacao_solicitacao) as dt_resposta_4
  41.             ,(select dt_resposta from setores_resposta where linha = 5 and id_solicitacao = tb017.id_notificacao_solicitacao) as dt_resposta_5
  42.           ,REPLACE(to_char(((select dt_resposta from setores_resposta where linha = 1 and id_solicitacao = tb017.id_notificacao_solicitacao) - dte.dt_encaminhamento )::interval,'dd HH24:MI:SS'), ' ', ' dia(s) ') AS tempo_resposta_setor_1    
  43.             ,REPLACE(to_char(((select dt_resposta from setores_resposta where linha = 2 and id_solicitacao = tb017.id_notificacao_solicitacao) - dte.dt_encaminhamento )::interval,'dd HH24:MI:SS'), ' ', ' dia(s) ') AS tempo_resposta_setor_2  
  44.             ,REPLACE(to_char(((select dt_resposta from setores_resposta where linha = 3 and id_solicitacao = tb017.id_notificacao_solicitacao) - dte.dt_encaminhamento )::interval,'dd HH24:MI:SS'), ' ', ' dia(s) ') AS tempo_resposta_setor_3  
  45.             ,REPLACE(to_char(((select dt_resposta from setores_resposta where linha = 4 and id_solicitacao = tb017.id_notificacao_solicitacao) - dte.dt_encaminhamento )::interval,'dd HH24:MI:SS'), ' ', ' dia(s) ') AS tempo_resposta_setor_4  
  46.             ,REPLACE(to_char(((select dt_resposta from setores_resposta where linha = 5 and id_solicitacao = tb017.id_notificacao_solicitacao) - dte.dt_encaminhamento )::interval,'dd HH24:MI:SS'), ' ', ' dia(s) ') AS tempo_resposta_setor_5  
  47.             ,frm.de_tema as tema
  48.             ,frm.de_assunto as assunto
  49.             ,frm.de_sub_assunto as sub_assunto
  50.             ,frm.de_tipo as tipo
  51.             ,frm.de_reincidente as prioridade
  52.             ,TB008.de_meio_comunicacao as CANAL_ATENDIMENTO
  53.             ,TB017.ID_NOTIFICACAO_SOLICITACAO AS NU_PROTOCOLO
  54.             ,(CASE WHEN TB009.DE_PESSOA IS NULL THEN
  55.                     'Anônimo'
  56.                   ELSE
  57.                     fnnormalizatexto(UPPER(TB009.DE_PESSOA),'')
  58.               END) AS DE_SOLICITANTE
  59.             ,fnnormalizatexto(fnnormalizacpf(TB009.DE_CPF),'') AS DE_SOLICITANTE_CPF
  60.             ,fnnormalizatexto(fnretornatelefone(TB009.ID_PESSOA::INTEGER),'') AS DE_SOLICITANTE_TELEFONE
  61.             ,TB009.DE_EMAIL AS DE_SOLICITANTE_EMAIL
  62.             ,fntabela_secaoarvore(TB056.ID_SECAO::INTEGER)::TEXT as ASSUNTO
  63.             ,to_char(TB017.DT_CADASTRO at time zone  'Brazil/East', 'DD/MM/YYYY HH24:MI:SS') AS DT_ABERTURA
  64.             ,date_part('month', TB017.DT_CADASTRO::timestamp)::INTEGER AS MES_ABERTURA
  65.             ,date_part('year', TB017.DT_CADASTRO::timestamp)::INTEGER AS ANO_ABERTURA
  66.             ,fnnormalizatexto(removetaghtml(fnnormalizahtml(TB017.DE_NOTIFICACAO)), '') AS DE_DESCRICAO_SOLICITACAO
  67.             ,(CASE
  68.                     WHEN TB017.ST_STATUS = 2 THEN
  69.                     'FINALIZADO'
  70.                     ELSE    
  71.                     fnnormalizatexto(TB016.DE_STATUS_NOTIFICACAO, 'RECEBIDO')
  72.             END) AS DE_STATUS_NOTIFICACAO
  73.             ,to_char(TH001_ENCAMINHAMENTO.DT_ALTERACAO at time zone  'Brazil/East', 'DD/MM/YYYY HH24:MI:SS') AS DT_ENCAMINHAMENTO
  74.             ,date_part('month', TH001_ENCAMINHAMENTO.DT_ALTERACAO::timestamp)::INTEGER AS MES_ENCAMINHAMENTO
  75.             ,date_part('year', TH001_ENCAMINHAMENTO.DT_ALTERACAO::timestamp)::INTEGER AS ANO_ENCAMINHAMENTO
  76.             ,fnnormalizatexto(UPPER(TB005.DE_USUARIO), '') AS DE_USUARIO
  77.             ,TB041.DE_ORGAO AS DE_ORGAO
  78.             ,fnnormalizatexto(removetaghtml(fnnormalizahtml(TH001.DE_RESPOSTA)), '') AS DE_RESPOSTA
  79.             ,to_char(TH001.DT_ALTERACAO at time zone 'Brazil/East', 'DD/MM/YYYY HH24:MI:SS') as DT_RESPOSTA
  80.             ,REPLACE(to_char((CURRENT_TIMESTAMP::TIMESTAMP - TB017.DT_CADASTRO::TIMESTAMP)::interval,'dd HH24:MI:SS'), ' ', ' dia(s) ') AS MASCARA_INTERVALO
  81.  
  82.  
  83.  
  84. FROM TB017_NOTIFICACAO_SOLICITACAO AS TB017
  85. LEFT JOIN
  86.     TB009_PESSOA AS TB009
  87.         ON
  88.             TB009.ID_PESSOA = TB017.ID_PESSOA  
  89. LEFT JOIN
  90.     TB056_DADO_SOLICITACAO AS TB056
  91.         ON
  92.             TB056.ID_DADO_SOLICITACAO = TB017.ID_DADO_SOLICITACAO
  93. LEFT JOIN
  94.     TB041_ORGAO AS TB041
  95.         ON
  96.             TB041.ID_ORGAO = TB017.ID_ORGAO
  97. LEFT JOIN
  98.     TB057_DADO_SOLICITACAO_INTERNACIONALIZA AS TB057
  99.         ON
  100.             TB057.ID_DADO_SOLICITACAO = TB056.ID_DADO_SOLICITACAO  
  101.             AND
  102.             TB057.DE_LOCALE = 'PORTUGUES'
  103. LEFT OUTER JOIN
  104.     TH001_NOTIFICACAO_SOLICITACAO_STATUS AS TH001
  105.         ON
  106.             TH001.ID_NOTIFICACAO_SOLICITACAO_STATUS = TB017.ID_HISTORICO_ATUAL
  107. LEFT JOIN
  108.     th001_notificacao_solicitacao_status AS TH001_ENCAMINHAMENTO
  109.         ON
  110.             TB017.ID_NOTIFICACAO_SOLICITACAO = TH001_ENCAMINHAMENTO.ID_NOTIFICACAO_SOLICITACAO
  111.             AND TH001_ENCAMINHAMENTO.id_notificacao_solicitacao_status = (select MAX(id_notificacao_solicitacao_status)
  112.                                                                                                                                         from th001_notificacao_solicitacao_status
  113.                                                                                                                                         where ID_STATUS_NOTIFICACAO_SOLICITACAO_ATUAL = 16
  114.                                                                                                                                             AND id_notificacao_solicitacao = TB017.ID_NOTIFICACAO_SOLICITACAO)
  115. LEFT JOIN TB005_USUARIO AS TB005
  116.   ON TB005.ID_USUARIO = TH001.ID_USUARIO_ALTERACAO
  117. LEFT OUTER JOIN
  118.     TB005_USUARIO AS TB005_ATRIBUIDO
  119.         ON
  120.             TB005_ATRIBUIDO.ID_USUARIO = TB017.ID_USUARIO_TRATAMENTO
  121. LEFT OUTER JOIN
  122.     TB016_STATUS_NOTIFICACAO AS TB016
  123.         ON
  124.             TB016.ID_STATUS_NOTIFICACAO = TH001.ID_STATUS_NOTIFICACAO_SOLICITACAO_ATUAL
  125. LEFT OUTER JOIN
  126.     TB008_MEIO_COMUNICACAO AS TB008
  127.         ON
  128.             TB008.ID_MEIO_COMUNICACAO = TB017.ID_MEIO_COMUNICACAO
  129.            
  130.            
  131.  JOIN frm_formulario_check_list_v2 AS frm
  132.         ON frm.id = tb017.id_registro_check_list
  133.        
  134. left JOIN setores_resposta as sr
  135.     ON sr.id_solicitacao = tb017.id_notificacao_solicitacao
  136. left join data_encaminhamento as dte
  137.     on sr.id_solicitacao = dte.id_solicitacao
  138.        
  139.  
  140. WHERE
  141. --tb017.id_notificacao_solicitacao = 18
  142. 1=1
  143. and
  144.     (
  145.         (
  146.             0 <> 0
  147.             AND
  148.             TB017.ID_NOTIFICACAO_SOLICITACAO = 0
  149.         )
  150.         OR
  151.         (
  152.             0 = 0
  153.             AND
  154.             (
  155.                 '--TODOS--' = '--TODOS--'
  156.                 OR
  157.                 fnNormalizaTexto(TB016.DE_STATUS_NOTIFICACAO, 'RECEBIDO') = '--TODOS--'
  158.             )
  159.             AND
  160.             (TB017.DT_CADASTRO at time zone 'Brazil/East')::DATE BETWEEN '2019-07-02'::DATE AND '2019-07-02'::DATE
  161.             AND
  162.             (
  163.               '--TODOS--' = '--TODOS--'
  164.               OR
  165.               (
  166.                 '--TODOS--' <> '--TODOS--'
  167.                 AND
  168.                 TB041.DE_ORGAO = '--TODOS--'
  169.               )
  170.             )
  171.         )
  172.     )
  173. ORDER BY TB017.ID_NOTIFICACAO_SOLICITACAO ASC
  174.  
  175. --limit 10;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement