创建基于sql查询的流过滤器

时间:2018-09-17 08:21:15

标签: sql sql-server microsoft-dynamics navision

我正在尝试基于流过滤器计算公式创建sql查询代码

所以我有一个表“ Vendor Ledger Entry”,其中有两个重要的字段,分别为“ Amount”和“ Remaining Amount”。

两个字段都是流过滤器,是根据公式与其他表格(在本例中为“详细的供应商分类帐”条目

)计算得出的

剩余金额:"Sum("Detailed Vendor Ledg. Entry".Amount WHERE (Vendor Ledger Entry No.=FIELD(Entry No.),Posting Date=FIELD(Date Filter),Excluded from calculation=CONST(No)))"

因此我将“剩余金额”“转换”为该sql查询

   Select Sum([E18375$Detailed Vendor Ledg_ Entry].[Amount]) as 'Valor pendente'
    FROM [E18375$Detailed Vendor Ledg_ Entry]
    INNER JOIN [E18375$Vendor Ledger Entry] 
ON [E18375$Detailed Vendor Ledg_ Entry].[Vendor Ledger Entry No_] = [E18375$Vendor Ledger Entry].[Entry No_]
    WHERE [E18375$Detailed Vendor Ledg_ Entry].[Excluded from calculation] = 0 
AND [E18375$Detailed Vendor Ledg_ Entry].[Document No_]='1707NC006' 
AND YEAR([E18375$Detailed Vendor Ledg_ Entry].[Posting Date])>=2017

问题是我得到的值“ Valor pendente”等于“ 1838”,而不是“ 10”,如下图所示。

enter image description here

我遵循在网上找到的这个示例来创建我的sql查询。

Sum("Detailed Cust. Ledg. Entry"."Amount (LCY)" WHERE (Cust. Ledger Entry No.=FIELD(Entry No.),Entry Type=FILTER(Initial Entry),Posting Date=FIELD(Date Filter)))

Select Sum([Amount (LCY)] FROM [Detailed Cust. Ledg. Entry]
INNER JOIN
[Cust. Ledg. Entry]
ON
[Detailed Cust. Ledg. Entry].[Entry No.] = [Cust. Ledg. Entry].[Entry No.]
WHERE
[Detailed Cust. Ledg. Entry].[Entry Type] = "Initial Entry"

我的完整查询如下:

select 'E18375' as Empresa, [E18375$Detailed Vendor Ledg_ Entry].[Posting Date],
[E18375$Detailed Vendor Ledg_ Entry].[Document No_], 
[E18375$Detailed Vendor Ledg_ Entry].[Vendor No_], 
[E18375$Detailed Vendor Ledg_ Entry].Amount, (Select Sum([Amount (LCY)])
FROM [E18375$Detailed Vendor Ledg_ Entry]
INNER JOIN [E18375$Vendor Ledger Entry] ON [E18375$Detailed Vendor Ledg_ Entry].[Vendor Ledger Entry No_] = [E18375$Vendor Ledger Entry].[Entry No_] 
WHERE [E18375$Detailed Vendor Ledg_ Entry].[Excluded from calculation]=0 
AND [E18375$Detailed Vendor Ledg_ Entry].[Amount]>0) as 'Valor pendente'
from [E18375$Vendor Ledger Entry] inner join [E18375$Detailed Vendor Ledg_ Entry] on [E18375$Detailed Vendor Ledg_ Entry].[Vendor Ledger Entry No_]=[E18375$Vendor Ledger Entry] .[Entry No_] 
where [E18375$Vendor Ledger Entry].[Open]=1 and [E18375$Detailed Vendor Ledg_ Entry].[Document No_]='1707NC006'
group by [E18375$Detailed Vendor Ledg_ Entry].[Posting Date], [E18375$Detailed Vendor Ledg_ Entry].[Document No_],
 [E18375$Detailed Vendor Ledg_ Entry].[Vendor No_], [E18375$Detailed Vendor Ledg_ Entry].[Vendor Ledger Entry No_], 
[E18375$Detailed Vendor Ledg_ Entry].[Amount]
having sum([E18375$Detailed Vendor Ledg_ Entry].[Amount])>0

2 个答案:

答案 0 :(得分:0)

所以您似乎只对2个表感兴趣:

[E18375 $详细的供应商分类帐_条目] [E18375 $供应商分类帐条目]

您的查询中似乎缺少[Entry Type] =“ Initial Entry”:

Select Sum([E18375$Detailed Vendor Ledg_ Entry].[Amount]) as 'Valor pendente'
FROM [E18375$Detailed Vendor Ledg_ Entry]
INNER JOIN [E18375$Vendor Ledger Entry] ON [E18375$Detailed Vendor Ledg_ Entry].[Entry No_] = [E18375$Vendor Ledger Entry].[Entry No_]
WHERE [E18375$Detailed Vendor Ledg_ Entry].[Excluded from calculation] = 0
AND [E18375$Detailed Vendor Ledg_ Entry].[Entry Type] = "Initial Entry";

我猜您的一个表中有一个记录,而另一个表中有很多记录,并且是过滤无法正常工作。

答案 1 :(得分:0)

我要走了,因为写的查询没有多大意义。但是我想您想获得每一行的明细行以及供应商的总数。

这可以做到:

foreach ( $some_iterator as $value ) {
    $some_callback($some_iterator);
}