select SE5.E5_FILIAL, FKA.FKA_FILIAL, MPA.E5_FILIAL // Função COALESCE
from SE5T10 SE5
left join FKAT10 FKA on ... // Utilizado para o relacionamento com as tabelas FK's.
left join SE5T10 MPA on ... // Utilizado para relacionar os registros de compensação na própria SE5 Ex.: NF x PA
where SE5.E5_FILIAL = 'D MG 01 '
and (
(
SE5.E5_DTDIGIT between '19800101' and '19800101'
and SE5.E5_TIPODOC in ('DH', 'PA', 'RA', 'BA', 'VL', 'V2', 'AP', 'EP', 'PE', 'RF', 'IF', 'CP', 'TL', 'ES', 'TR', 'DB', 'OD', 'LJ', 'E2', 'TE', ' ', 'IT')
)
or (
SE5.E5_DATA between '19800101' and '19800101'
and SE5.E5_TIPODOC in ('TR', 'TE')
)
)
and SE5.E5_SITUACA <> 'C'
and (
SE5.E5_LA <> 'S '
or (
(SE5.E5_ORDREC || SE5.E5_SERREC) <> ' '
and SE5.E5_RECPAG = 'R'
and SE5.E5_TIPODOC = 'BA'
)
)
and SE5.E5_MOTBX not in ('DSD')
and SE5.D_E_L_E_T_ = ' '
and SE5.E5_RECONC = 'x'
order by E5_FILIAL, E5_DTDIGIT, E5_RECPAG, E5_NUMCHEQ, E5_DOCUMEN, E5_PREFIXO, E5_NUMERO, E5_PARCELA, E5_TIPO, E5_CLIFOR, E5_LOJA, E5_SEQ, SE5RECNO
|