簡體   English   中英

如何在NAV Dynamics(以前的Navision)中使用發票將SQL與付款合並

[英]How to SQL join payments with invoices in NAV Dynamics (former Navision)

問題是有關Dynamics Nav表中的SQL擺弄,尤其是表[$ 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表中。

桌子是

[Detailed Vendor Ledg. Entry]

帶有[Entry Type] = Application

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM