Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT DISTINCT
  2.         a.id, a.ano, t1.tramites, t2.tramites
  3. FROM tb_auto a
  4.     INNER JOIN tb_tipoauto AS ta ON ta.id = a.tipoauto_id
  5.     LEFT JOIN tb_autoendereco AS ae ON ae.auto_id = a.id
  6.     LEFT JOIN tb_auto_pessoa ap ON ap.auto_id = a.id
  7.     left JOIN (select array_agg(tramite_id) as tramites, auto_id from tb_auto_tramites group by auto_id) AS t1 ON t1.auto_id = a.id
  8.     left JOIN (select array_agg(tramite_id) as tramites, auto_id from tb_auto_tramites where data_tramite between '01/01/2014' and '31/12/2014'  group by auto_id) AS t2 ON t2.auto_id = a.id
  9.    
  10.     --inner join (select array_agg(t.tramite_id), auto_id from tb_auto_tramites as t where t.auto_id = a.id)
  11.    
  12.   --  INNER JOIN tb_auto_tramites as at on at.auto_id = a.
  13.     --LEFT JOIN relatorios.tb_pessoa_report AS rps ON rps.pessoa_id = ap.pessoa_id
  14.     --LEFT JOIN tb_auto_proprietario apr on a.id = apr.auto_id
  15.     --LEFT JOIN relatorios.tb_pessoa_report AS rpsp ON rpsp.pessoa_id = apr.pessoa_id
  16.     --LEFT JOIN tb_auto_anterior AS aa ON aa.auto_id = a.id
  17.     --LEFT JOIN tb_auto_fiscais af ON af.auto_id = a.id
  18.     --LEFT JOIN tb_fiscal f ON f.id = af.fiscal_id
  19.        
  20.     --INNER JOIN tb_auto_tramites at on at.auto_id = a.id  
  21.     --INNER JOIN tb_auto_protocolo AS auto_protocolo ON auto_protocolo.auto_id = a.id
  22.     --INNER JOIN tb_protocolo protocolo_ultimomov ON protocolo_ultimomov.id = auto_protocolo.protocolo_id
  23.     --INNER JOIN tb_movimento AS movimento ON movimento.id = protocolo_ultimomov.movimento_id
  24.            
  25. WHERE
  26.     a.data_cadastro::DATE BETWEEN '01/01/2013' AND '31/12/2013'
  27.     AND  (t2.tramites  @>  ARRAY[51])
  28.     AND  (t1.tramites  @>  ARRAY[45]) = FALSE  
  29.    
  30.     --and a.id = 687
  31.     --and array[t1.tramite_id] && array[66]
  32.  
  33. --ORDER BY a.ano, a.numero
  34. LIMIT 250
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement