Как SQL объединить платежи со счетами в NAV Dynamics (ранее Navision)

Вопрос в том, чтобы возиться с SQL в таблицах Dynamics Nav, особенно в таблице [$ Vendor Ledger Entry]. Как связать каждый платеж с соответствующими счетами.

Цель состоит в том, чтобы получить [Внешний номер документа_] и [Сумма] счета-фактуры. Один платеж может быть сопоставлен с несколькими счетами-фактурами (бывает) или один счет-фактура с несколькими платежами (редкий случай).

Мой отчаянный подход:

;with cte as 
(
select 
*
,[Row_max]=max([Row]) OVER(PARTITION BY CBEN,[Document Type])
from (
        select 
        *
        ,[Row]=ROW_NUMBER() OVER(PARTITION BY CBEN,[Document Type]  ORDER BY [Entry No_] asc)
        from (
                select
                *
                ,CBEN=case when [Closed by Entry No_]=0 then [Entry No_] else [Closed by Entry No_] end
                from [CompanyName$Vendor Ledger Entry] --here put correct table name
        ) tb1
        ) tb2
)

select 
 a.[Entry No_] [Entry No_payment] -- letter t means transfers
,b.[Entry No_] [Entry No_invoice] -- letter p means payables
--,a.CBEN as CBEN_t
--,b.CBEN as CBEN_p
--,a.[Row_max] as Row_max_t
--,b.[Row_max] as Row_max_p
--,a.[Row] as Row_t
--,b.[Row] as Row_p
,a.[Open]
,b.[External Document No_] [External Document No_invoice]
,a.[Document No_] [Document No_payment]
,b.[Document No_] [Document No_invoice]
,b.[Document Date]
,b.[Posting Date]
,b.[Due Date]
,Amount_p=b.[Closed by Amount]
--,a.[Company]
from cte a

left join cte b
on 
(
a.CBEN=b.CBEN and -- join using Closed By Entry No
(a.[Row]=b.[Row] and a.[Row_max]=b.[Row_max] or
 a.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1) or 
 b.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1)
) and 
a.[Document Type] in (0,1) and
b.[Document Type]=2
and a.[Open]=0
)
or
(
a.CBEN = b.[Entry No_] and -- or join by Closed By Entry No to EntryNo
(a.[Row]=b.[Row] and a.[Row_max]=b.[Row_max] or
 a.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1) or 
 b.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1)
) and 
a.[Document Type] in (0,1) and
b.[Document Type]=2
and a.[Open]=0
)

where 
a.[Open]=0 and a.[Document Type] in (0,1) and b.[Entry No_] is not null -- show payments with matched invoices
or 
a.[Open]=1 and a.[Document Type] in (0,1) and b.[Entry No_] is null     -- or payments with open status
order by a.CBEN,1,2,a.[Document No_]

Я затрудняюсь присоединиться к счету-фактуре [сумма], и я не знаю, в какой таблице Dynamics NAV это может быть.


person Przemyslaw Remin    schedule 06.05.2016    source источник


Ответы (1)


таблица

[Detailed Vendor Ledg. Entry]

с [Entry Type] = Application

person xdd    schedule 06.05.2016
comment
Вы имеете в виду столбец [Номер записи прикладной торговой книги_]? Какие еще ключи вы используете, если [Applied Vend_ Ledger Entry No_] совпадает со многими [Entry]? - person Przemyslaw Remin; 06.05.2016
comment
два поля [Номер записи в бухгалтерской книге поставщика] и [Заявленная продажа. Номер записи в бухгалтерской книге]. один из них - счет-фактура, второй - оплата. запись содержит сумму, которая не может быть полной суммой счета-фактуры или платежа - person xdd; 06.05.2016
comment
Не могли бы вы пролить свет на два других поля, которые могут иметь отношение к вопросу. (1) [Тип исходного документа] - для чего остаются разные номера? (2) [Не подано заявкой №_] - для чего это? - person Przemyslaw Remin; 09.05.2016