title: python library for extracting table names from from clause in SQL Statetments,
body: <p>i have a complex SQL like below. I'm looking for a python (3+) library that would extract the table names that are contained in the from clauses. I'm mapping a client's database &amp; views &amp; reports, and have dozens of queries to map, so the need to automate the job.</p>
  <p>Already looked at Python-sqlparse , sqlparse, but I'm not sure they are up to the task and I'm trying to avoid the learning curve.</p>
  <pre><code>SELECT --Locais Disponíveis para Abertura de Ordens de corte
       COALESCE(a.&quot;Chave&quot;, b.&quot;Chave&quot;) AS &quot;Chave&quot;,
       COALESCE(a.&quot;Unidade&quot;, b.&quot;Unidade&quot;) AS &quot;Unidade&quot;,
       COALESCE(a.&quot;Fazenda&quot;, b.&quot;Fazenda&quot;) AS &quot;Fazenda&quot;,
       COALESCE(a.&quot;Talhao&quot;, b.&quot;Talhao&quot;) AS &quot;Talhao&quot;,
       COALESCE(a.&quot;Participacao&quot;, b.&quot;Participacao&quot;) AS &quot;Participacao&quot;,
       CASE
         WHEN a.&quot;Condicao&quot; = 'Disponível Parcial (Moagem)' AND
              b.&quot;Condicao&quot; = 'Disponível Parcial (Mudas)' THEN
          'Disponível (Safra+Mudas)'
         ELSE
          COALESCE(a.&quot;Condicao&quot;, b.&quot;Condicao&quot;)
       END AS &quot;Condicao&quot;,
       COALESCE(a.&quot;Estagio&quot;, b.&quot;Estagio&quot;) AS &quot;Estagio&quot;,
       COALESCE(a.&quot;Variedade&quot;, b.&quot;Variedade&quot;) AS &quot;Variedade&quot;,
       COALESCE(a.&quot;Ciclo Maturacao&quot;, b.&quot;Ciclo Maturacao&quot;) AS &quot;Ciclo Maturacao&quot;,
       COALESCE(a.&quot;Propriedade&quot;, b.&quot;Propriedade&quot;) AS &quot;Propriedade&quot;,
       COALESCE(a.&quot;Proprietario&quot;, b.&quot;Proprietario&quot;) AS &quot;Proprietario&quot;,
       COALESCE(a.&quot;No. Corte&quot;, b.&quot;No. Corte&quot;) AS &quot;No. Corte&quot;,
       (CASE
         WHEN a.&quot;Area&quot; IS NULL THEN
      0
         ELSE
          a.&quot;Area&quot;
       END + CASE
         WHEN b.&quot;Area&quot; IS NULL THEN
      0
         ELSE
          b.&quot;Area&quot;
       END) AS &quot;Area&quot;,
       CASE
         WHEN a.&quot;Condicao&quot; = 'Disponível Parcial (Moagem)' AND
              b.&quot;Condicao&quot; = 'Disponível Parcial (Mudas)' THEN
          ((CASE
            WHEN a.&quot;Area&quot; IS NULL THEN
      0
            ELSE
             a.&quot;Area&quot;
          END + CASE
            WHEN b.&quot;Area&quot; IS NULL THEN
      0
            ELSE
             b.&quot;Area&quot;
          END) * a.&quot;TCH&quot;)
         ELSE
          a.&quot;Toneladas&quot;
       END AS &quot;Toneladas&quot;,
       a.&quot;TCH&quot;,
       COALESCE(a.&quot;Distancia&quot;, b.&quot;Distancia&quot;) AS &quot;Distancia&quot;
        FROM (SELECT --Disponibilidade (Moagem)
               A.*,
               a.&quot;Area&quot; * b.&quot;TCH&quot; AS &quot;Toneladas&quot;,
               b.&quot;TCH&quot; AS &quot;TCH&quot;,
               c.&quot;Dist. Terra&quot; + c.&quot;Dist. Asfalto&quot; AS &quot;Distancia&quot;
                FROM ((SELECT --ÁREAS DISPONÍVEIS PARA ABERTURA DE ORDEM CORTE DE SAFRA
                        a.&quot;Fazenda&quot; * 1000 + a.&quot;Talhao&quot; AS &quot;Chave&quot;,
                        CASE
                          WHEN a.&quot;Unidade&quot; = 15 THEN
                           'USF'
                          ELSE
                           'URD'
                        END AS &quot;Unidade&quot;,
                        a.&quot;Fazenda&quot;,
                        a.&quot;Talhao&quot;,
                        a.&quot;Participacao&quot;,
                        CASE
                          WHEN a.&quot;Ocorrencia Cadastro&quot; = 'C' THEN
                           'Disponível Total (Moagem)'
                          ELSE
                           'Disponível Parcial (Moagem)'
                        END AS &quot;Condicao&quot;,
                        a.&quot;Estagio&quot;,
                        a.&quot;Variedade&quot;,
                        a.&quot;Ciclo Maturacao&quot;,
                        a.&quot;Propriedade&quot;,
                        a.&quot;Proprietario&quot;,
                        a.&quot;No. Corte&quot;,
                        (a.&quot;Area&quot; - (CASE
                          WHEN b.&quot;Area Fechada&quot; IS NULL THEN
      0
                          ELSE
                           b.&quot;Area Fechada&quot;
                        END)) AS &quot;Area&quot;
                         FROM (SELECT --ULTIMA ESTIMATIVA DO TALHAO A
                                OBJ.CD_UNID_IND AS &quot;Unidade&quot;,
                                OBJ.CD_UPNIVEL1 AS &quot;Fazenda&quot;,
                                OBJ.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                                OBJ.CD_UPNIVEL1 || ' - ' || F.DE_UPNIVEL1 AS &quot;Propriedade&quot;,
                                G.DE_FORNEC AS &quot;Proprietario&quot;,
                                CASE
                                  WHEN UP3.CD_TP_PROPR IN (1, 2, 3, 11) THEN
                                   'Parceria'
                                  WHEN UP3.CD_TP_PROPR IN (5, 8) THEN
                                   'Fornecedor'
                                  WHEN UP3.CD_TP_PROPR = 6 THEN
                                   'Fornecedor'
                                  WHEN UP3.CD_TP_PROPR = 14 THEN
                                   'Parceria'
                                  ELSE
                                   'Verificar'
                                END AS &quot;Participacao&quot;,
                                C.FG_OCORREN AS &quot;Ocorrencia Cadastro&quot;,
                                C.DT_OCORREN AS &quot;Data Ocorrencia&quot;,
                                B.DA_ESTAGIO AS &quot;Estagio&quot;,
                                B.NO_CORTE AS &quot;No. Corte&quot;,
                                D.DE_VARIED AS &quot;Variedade&quot;,
                                E.DE_MATURAC AS &quot;Ciclo Maturacao&quot;,
                                (OBJ.QT_AREA_PROD * 1) AS &quot;Area&quot;,
                                (OBJ.QT_CANA_ENTR / 1000) AS &quot;Toneladas&quot;
                                 FROM PIMSCS.HISTPREPRO   OBJ,
                                      PIMSCS.ESTAGIOS     B,
                                      PIMSCS.UPNIVEL3     UP3,
                                      PIMSCS.SAFRUPNIV3   C,
                                      PIMSCS.VARIEDADES   D,
                                      PIMSCS.TIPO_MATURAC E,
                                      PIMSCS.UPNIVEL1     F,
                                      PIMSCS.FORNECS      G
                                WHERE OBJ.CD_SAFRA =
                                      (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                                  AND OBJ.CD_UNID_IND IN (15, 19)
                                  AND OBJ.CD_ESTAGIO = B.CD_ESTAGIO
                                  AND OBJ.CD_UPNIVEL1 = UP3.CD_UPNIVEL1
                                  AND OBJ.CD_UPNIVEL3 = UP3.CD_UPNIVEL3
                                  AND OBJ.CD_SAFRA = UP3.CD_SAFRA
                                  AND OBJ.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                  AND OBJ.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                  AND OBJ.CD_SAFRA = C.CD_SAFRA
                                  AND UP3.CD_VARIED = D.CD_VARIED
                                  AND E.FG_MATURAC = D.FG_MATURAC
                                  AND OBJ.CD_UPNIVEL1 = F.CD_UPNIVEL1
                                  AND F.CD_FORNEC = G.CD_FORNEC
                                  AND C.DT_OCORREN =
                                      (SELECT MAX(D.DT_OCORREN)
                                         FROM PIMSCS.SAFRUPNIV3 D
                                        WHERE D.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                          AND D.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                          AND D.CD_SAFRA = C.CD_SAFRA)
                                  AND OBJ.CD_HIST =
                                      (SELECT OBJ2.CD_HIST
                                         FROM PIMSCS.HISTPREPRO OBJ2
                                        WHERE OBJ2.CD_UPNIVEL1 = OBJ.CD_UPNIVEL1
                                          AND OBJ2.CD_UPNIVEL3 = OBJ.CD_UPNIVEL3
                                          AND OBJ2.CD_SAFRA =
                                              (SELECT MAX(CD_SAFRA)
                                                 FROM PIMSCS.HISTPREPRO)
                                          AND OBJ2.CD_HIST NOT IN ('E', 'S')
                                          AND OBJ2.CD_EMPRESA IN (15, 19)
                                          AND OBJ2.DT_HISTORICO =
                                              (SELECT MAX(OBJ3.DT_HISTORICO)
                                                 FROM PIMSCS.HISTPREPRO OBJ3
                                                WHERE OBJ3.CD_UPNIVEL1 =
                                                      OBJ.CD_UPNIVEL1
                                                  AND OBJ3.CD_UPNIVEL3 =
                                                      OBJ.CD_UPNIVEL3
                                                  AND OBJ3.CD_SAFRA =
                                                      (SELECT MAX(CD_SAFRA)
                                                         FROM PIMSCS.HISTPREPRO)
                                                  AND OBJ3.CD_HIST NOT IN ('E', 'S')
                                                  AND OBJ3.CD_EMPRESA IN (15, 19)))) A,
                              (SELECT --ÁREA DE ORDEM DE CORTE DE SAFRA FECHADA B
                                QD.CD_UPNIVEL1 AS &quot;Fazenda&quot;,
                                QD.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                                SUM(QD.QT_AREA) AS &quot;Area Fechada&quot;
                                 FROM PIMSCS.QUEIMA_HE QH, PIMSCS.QUEIMA_DE QD
                                WHERE QH.NO_QUEIMA = QD.NO_QUEIMA
                                  AND QD.CD_SAFRA =
                                      (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                                GROUP BY QD.CD_UPNIVEL1, QD.CD_UPNIVEL3) B
                        WHERE a.&quot;Fazenda&quot; = b.&quot;Fazenda&quot;(+)
                          AND a.&quot;Talhao&quot; = b.&quot;Talhao&quot;(+)
                          AND a.&quot;Ocorrencia Cadastro&quot; &lt;&gt; 'F'
                          AND (a.&quot;Area&quot; - (CASE
                                WHEN b.&quot;Area Fechada&quot; IS NULL THEN
      0
                                ELSE
                                 b.&quot;Area Fechada&quot;
                              END)) &gt; 0)) A
                LEFT JOIN (SELECT --Ultima Estimativa do Talhão
                           A.CD_HIST &quot;Cod. Historico&quot;,
                           CASE
                             WHEN A.CD_UNID_IND = 15 THEN
                              'USF'
                             ELSE
                              'URD'
                           END AS &quot;Unidade&quot;,
                           A.CD_UPNIVEL1 AS &quot;Zona&quot;,
                           A.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                           A.DT_HISTORICO AS &quot;Data&quot;,
                           A.QT_AREA_PROD AS &quot;Area&quot;,
                           (A.QT_CANA_ENTR / 1000) AS &quot;Toneladas&quot;,
                           A.QT_TCH AS &quot;TCH&quot;
                            FROM PIMSCS.HISTPREPRO A
                           WHERE A.CD_UNID_IND IN (15, 19)
                             AND A.CD_SAFRA =
                                 (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                             AND A.CD_HIST NOT IN ('E', 'S')
                             AND A.QT_AREA_PROD &lt;&gt; 0
                             AND A.DT_HISTORICO =
                                 (SELECT MAX(A2.DT_HISTORICO)
                                    FROM PIMSCS.HISTPREPRO A2
                                   WHERE A.CD_SAFRA = A2.CD_SAFRA
                                     AND A.CD_UPNIVEL2 = A2.CD_UPNIVEL1
                                     AND A.CD_UPNIVEL3 = A2.CD_UPNIVEL3
                                     AND A2.CD_HIST NOT IN ('E', 'S'))) B
                  ON a.&quot;Fazenda&quot; = b.&quot;Zona&quot;
                 AND a.&quot;Talhao&quot; = b.&quot;Talhao&quot;
                LEFT JOIN (SELECT --Distancia Cadastrada 
                           A.CD_UPNIVEL1 AS &quot;Zona&quot;,
                           A.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                           MAX(A.DS_TERRA) AS &quot;Dist. Terra&quot;,
                           MAX(A.DS_ASFALTO) AS &quot;Dist. Asfalto&quot;
                            FROM PIMSCS.UPNIVEL3 A
                            LEFT JOIN PIMSCS.SAFRUPNIV3 B
                              ON A.CD_SAFRA = B.CD_SAFRA
                             AND A.CD_UPNIVEL1 = B.CD_UPNIVEL1
                             AND A.CD_UPNIVEL3 = B.CD_UPNIVEL3
                           WHERE A.CD_UNID_IND IN (15, 19)
                             AND A.CD_OCUP = 1
                             AND A.CD_SAFRA =
                                 (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                             AND B.CD_SAFRA =
                                 (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                             AND B.FG_OCORREN &lt;&gt; 'I'
                             AND B.DT_OCORREN =
                                 (SELECT MAX(B2.DT_OCORREN)
                                    FROM PIMSCS.SAFRUPNIV3 B2
                                   WHERE B.CD_SAFRA = B2.CD_SAFRA
                                     AND B.CD_UPNIVEL1 = B2.CD_UPNIVEL1
                                     AND B.CD_UPNIVEL3 = B2.CD_UPNIVEL3)
                           GROUP BY A.CD_UPNIVEL1, A.CD_UPNIVEL3) C
                  ON a.&quot;Fazenda&quot; = c.&quot;Zona&quot;
                 AND a.&quot;Talhao&quot; = c.&quot;Talhao&quot;) A
        FULL JOIN (SELECT --Disponibilidade (Mudas)
                    A.*,
                    a.&quot;Area&quot; * b.&quot;TCH&quot; AS &quot;Toneladas&quot;,
                    b.&quot;TCH&quot; AS &quot;TCH&quot;,
                    c.&quot;Dist. Terra&quot; + c.&quot;Dist. Asfalto&quot; AS &quot;Distancia&quot;
                     FROM ((SELECT --ÁREAS DISPONÍVEIS PARA ABERTURA DE ORDEM CORTE DE SAFRA
                             a.&quot;Fazenda&quot; * 1000 + a.&quot;Talhao&quot; AS &quot;Chave&quot;,
                             CASE
                               WHEN a.&quot;Unidade&quot; = 15 THEN
                                'USF'
                               ELSE
                                'URD'
                             END AS &quot;Unidade&quot;,
                             a.&quot;Fazenda&quot;,
                             a.&quot;Talhao&quot;,
                             a.&quot;Participacao&quot;,
                             CASE
                               WHEN a.&quot;Ocorrencia Cadastro&quot; = 'C' THEN
                                'Disponível Total (Mudas)'
                               ELSE
                                'Disponível Parcial (Mudas)'
                             END AS &quot;Condicao&quot;,
                             a.&quot;Estagio&quot;,
                             a.&quot;Variedade&quot;,
                             a.&quot;Ciclo Maturacao&quot;,
                             a.&quot;Propriedade&quot;,
                             a.&quot;Proprietario&quot;,
                             a.&quot;No. Corte&quot;,
                             (a.&quot;Area&quot; - (CASE
                               WHEN b.&quot;Area Fechada&quot; IS NULL THEN
      0
                               ELSE
                                b.&quot;Area Fechada&quot;
                             END)) AS &quot;Area&quot;
                              FROM (SELECT --ULTIMA ESTIMATIVA DO TALHAO A
                                     OBJ.CD_UNID_IND AS &quot;Unidade&quot;,
                                     OBJ.CD_UPNIVEL1 AS &quot;Fazenda&quot;,
                                     OBJ.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                                     OBJ.CD_UPNIVEL1 || ' - ' || F.DE_UPNIVEL1 AS &quot;Propriedade&quot;,
                                     G.DE_FORNEC AS &quot;Proprietario&quot;,
                                     CASE
                                       WHEN UP3.CD_TP_PROPR IN (1, 2, 3, 11) THEN
                                        'Parceria'
                                       WHEN UP3.CD_TP_PROPR IN (5, 8) THEN
                                        'Fornecedor'
                                       WHEN UP3.CD_TP_PROPR = 6 THEN
                                        'Fornecedor'
                                       WHEN UP3.CD_TP_PROPR = 14 THEN
                                        'Parceria'
                                       ELSE
                                        'Verificar'
                                     END AS &quot;Participacao&quot;,
                                     C.FG_OCORREN AS &quot;Ocorrencia Cadastro&quot;,
                                     C.DT_OCORREN AS &quot;Data Ocorrencia&quot;,
                                     B.DA_ESTAGIO AS &quot;Estagio&quot;,
                                     B.NO_CORTE AS &quot;No. Corte&quot;,
                                     D.DE_VARIED AS &quot;Variedade&quot;,
                                     E.DE_MATURAC AS &quot;Ciclo Maturacao&quot;,
                                     (OBJ.QT_AREA_PROD * 1) AS &quot;Area&quot;,
                                     (OBJ.QT_CANA_ENTR / 1000) AS &quot;Toneladas&quot;
                                      FROM PIMSCS.HISTPREPRO   OBJ,
                                           PIMSCS.ESTAGIOS     B,
                                           PIMSCS.UPNIVEL3     UP3,
                                           PIMSCS.SAFRUPNIV3   C,
                                           PIMSCS.VARIEDADES   D,
                                           PIMSCS.TIPO_MATURAC E,
                                           PIMSCS.UPNIVEL1     F,
                                           PIMSCS.FORNECS      G
                                     WHERE OBJ.CD_SAFRA =
                                           (SELECT MAX(CD_SAFRA)
                                              FROM PIMSCS.HISTPREPRO)
                                       AND OBJ.CD_UNID_IND IN (15, 19)
                                       AND OBJ.CD_ESTAGIO = B.CD_ESTAGIO
                                       AND OBJ.CD_UPNIVEL1 = UP3.CD_UPNIVEL1
                                       AND OBJ.CD_UPNIVEL3 = UP3.CD_UPNIVEL3
                                       AND OBJ.CD_SAFRA = UP3.CD_SAFRA
                                       AND OBJ.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                       AND OBJ.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                       AND OBJ.CD_SAFRA = C.CD_SAFRA
                                       AND UP3.CD_VARIED = D.CD_VARIED
                                       AND E.FG_MATURAC = D.FG_MATURAC
                                       AND OBJ.CD_UPNIVEL1 = F.CD_UPNIVEL1
                                       AND F.CD_FORNEC = G.CD_FORNEC
                                       AND C.DT_OCORREN =
                                           (SELECT MAX(D.DT_OCORREN)
                                              FROM PIMSCS.SAFRUPNIV3 D
                                             WHERE D.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                               AND D.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                               AND D.CD_SAFRA = C.CD_SAFRA)
                                       AND OBJ.CD_HIST =
                                           (SELECT OBJ2.CD_HIST
                                              FROM PIMSCS.HISTPREPRO OBJ2
                                             WHERE OBJ2.CD_UPNIVEL1 = OBJ.CD_UPNIVEL1
                                               AND OBJ2.CD_UPNIVEL3 = OBJ.CD_UPNIVEL3
                                               AND OBJ2.CD_SAFRA =
                                                   (SELECT MAX(CD_SAFRA)
                                                      FROM PIMSCS.HISTPREPRO)
                                               AND OBJ2.CD_HIST = 'S'
                                               AND OBJ2.CD_EMPRESA IN (15, 19)
                                               AND OBJ2.DT_HISTORICO =
                                                   (SELECT MAX(OBJ3.DT_HISTORICO)
                                                      FROM PIMSCS.HISTPREPRO OBJ3
                                                     WHERE OBJ3.CD_UPNIVEL1 =
                                                           OBJ.CD_UPNIVEL1
                                                       AND OBJ3.CD_UPNIVEL3 =
                                                           OBJ.CD_UPNIVEL3
                                                       AND OBJ3.CD_SAFRA =
                                                           (SELECT MAX(CD_SAFRA)
                                                              FROM PIMSCS.HISTPREPRO)
                                                       AND OBJ3.CD_HIST = 'S'
                                                       AND OBJ3.CD_EMPRESA IN (15, 19)))) A,
                                   (SELECT --ÁREA DE ORDEM DE CORTE DE MUDAS FECHADA B
                                     A.CD_UPNIVEL1 AS &quot;Fazenda&quot;,
                                     A.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                                     SUM(A.QT_AREA) AS &quot;Area Fechada&quot;
                                      FROM  PIMSCS.OCORTEMD_DE A
                                      JOIN PIMSCS.OCORTEMD_HE B
                                        ON A.NO_ORDEM = B.NO_ORDEM
                                     WHERE A.CD_SAFRA =
                                           (SELECT MAX(CD_SAFRA)
                                              FROM PIMSCS.HISTPREPRO)
                                       AND B.FG_SITUACAO = 'F'
                                     GROUP BY A.CD_UPNIVEL1, A.CD_UPNIVEL3) B
                             WHERE a.&quot;Fazenda&quot; = b.&quot;Fazenda&quot;(+)
                               AND a.&quot;Talhao&quot; = b.&quot;Talhao&quot;(+)
                               AND a.&quot;Ocorrencia Cadastro&quot; &lt;&gt; 'F'
                               AND (a.&quot;Area&quot; - (CASE
                                     WHEN b.&quot;Area Fechada&quot; IS NULL THEN
      0
                                     ELSE
                                      b.&quot;Area Fechada&quot;
                                   END)) &gt; 0)) A
                     LEFT JOIN (SELECT --Ultima Estimativa do Talhão
                                A.CD_HIST &quot;Cod. Historico&quot;,
                                CASE
                                  WHEN A.CD_UNID_IND = 15 THEN
                                   'USF'
                                  ELSE
                                   'URD'
                                END AS &quot;Unidade&quot;,
                                A.CD_UPNIVEL1 AS &quot;Zona&quot;,
                                A.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                                A.DT_HISTORICO AS &quot;Data&quot;,
                                A.QT_AREA_PROD AS &quot;Area&quot;,
                                (A.QT_CANA_ENTR / 1000) AS &quot;Toneladas&quot;,
                                A.QT_TCH AS &quot;TCH&quot;
                                 FROM PIMSCS.HISTPREPRO A
                                WHERE A.CD_UNID_IND IN (15, 19)
                                  AND A.CD_SAFRA =
                                      (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                                  AND A.CD_HIST = 'S'
                                  AND A.QT_AREA_PROD &lt;&gt; 0
                                  AND A.DT_HISTORICO =
                                      (SELECT MAX(A2.DT_HISTORICO)
                                         FROM PIMSCS.HISTPREPRO A2
                                        WHERE A.CD_SAFRA = A2.CD_SAFRA
                                          AND A.CD_UPNIVEL2 = A2.CD_UPNIVEL1
                                          AND A.CD_UPNIVEL3 = A2.CD_UPNIVEL3
                                          AND A2.CD_HIST = 'S')) B
                       ON a.&quot;Fazenda&quot; = b.&quot;Zona&quot;
                      AND a.&quot;Talhao&quot; = b.&quot;Talhao&quot;
                     LEFT JOIN (SELECT --Distancia Cadastrada 
                                A.CD_UPNIVEL1 AS &quot;Zona&quot;,
                                A.CD_UPNIVEL3 AS &quot;Talhao&quot;,
                                MAX(A.DS_TERRA) AS &quot;Dist. Terra&quot;,
                                MAX(A.DS_ASFALTO) AS &quot;Dist. Asfalto&quot;
                                 FROM PIMSCS.UPNIVEL3 A
                                 LEFT JOIN PIMSCS.SAFRUPNIV3 B
                                   ON A.CD_SAFRA = B.CD_SAFRA
                                  AND A.CD_UPNIVEL1 = B.CD_UPNIVEL1
                                  AND A.CD_UPNIVEL3 = B.CD_UPNIVEL3
                                WHERE A.CD_UNID_IND IN (15, 19)
                                  AND A.CD_OCUP = 1
                                  AND A.CD_SAFRA =
                                      (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                                  AND B.CD_SAFRA =
                                      (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                                  AND B.FG_OCORREN &lt;&gt; 'I'
                                  AND B.DT_OCORREN =
                                      (SELECT MAX(B2.DT_OCORREN)
                                         FROM PIMSCS.SAFRUPNIV3 B2
                                        WHERE B.CD_SAFRA = B2.CD_SAFRA
                                          AND B.CD_UPNIVEL1 = B2.CD_UPNIVEL1
                                          AND B.CD_UPNIVEL3 = B2.CD_UPNIVEL3)
                                GROUP BY A.CD_UPNIVEL1, A.CD_UPNIVEL3) C
                       ON a.&quot;Fazenda&quot; = c.&quot;Zona&quot;
                      AND a.&quot;Talhao&quot; = c.&quot;Talhao&quot;) B
          ON a.&quot;Chave&quot; = b.&quot;Chave&quot;
  
  </code></pre>