从左连接中删除重复记录

时间:2016-07-01 08:06:48

标签: sql-server-2014 microsoft-dynamics navision

我使用Microsoft Dynamics Navision 2013和SQL 2014作为数据源。 Value条目表是托管customers / Items的所有销售交易的主表。

对于我们支付消费税(税)的部分项目,我们支付从一个仓库转移到另一个仓库(我们称之为转移消费税)的所有物品的特定费率。

我在一个葡萄酒农场工作,每年我们为每个地点的每个项目输入一个单位费率(位置是葡萄酒在转出仓库之前存放的仓库)。我们的单位费率是在28-创建的02-28。

为了获得转移消费税的计算,我从Microsoft Dynamics Navision的各种表中获取了数据。这些表包含我所用的列:

Location : Code, also contains a filter “NONBOND”

Item: No(PK) , Excise Type

Excise Rate: Starting Date (The starting date of the Unit Rate), Excise Type Code

Item Unit Of Measure: Item No(PK), Qty_ per unit of measure as (Litre Conversion Factor)

将数据输入名为Transfer Excise Tbl 1的新表中。为了简化我的示例,我的Transfer Excise表包含:No_ and Location Code(FK)我的值条目表包含Item No_(PK),Location Code(PK)。我当然会加入Transfer ExciseValue Entry表格(No_=Item No_)和Location code = Location code

我正在尝试获取与Value Entry表格相匹配的所有项目交易,针对该特定开始日期的每个单位费率以及使用LEFT JOIN过滤器发布交易的过帐日期。

在值输入表格中,我使用Gen_ Bus_ Posting Group仅过滤(LOCA& EXSA),LOCA表示本地和EXSA含义南非出口。这是我正在尝试的查询,但是我得到重复项,没有开始日期和发布日期。我尝试使用DISTINCT,如我的查询中所示,但它不起作用。

SELECT DISTINCT  a.[Starting Date],  
                 b.[Posting Date], 
              b.[Item No_],  
           b.[Invoiced Quantity],  
           a.[Litre Conversion Factor], 
           a.[Unit Rate] , 
              a.[Location Code],  
           a.[Excise Location], 
           a.[Excise Type Code], 
           a.[Unit Of Measure Code]
FROM [Transfer Excise Tbl] a
LEFT JOIN [Spier Live$Value Entry] b
    ON a.[No_] = b.[Item No_] 

WHERE  b.[Posting Date] > '2014-03-01'
AND b.[Item No_] = 'F00335'
AND b.[Location Code] = a.[Location Code]
AND b.[Gen_ Bus_ Posting Group] IN ('LOCA','EXSA')
AND b.[Posting Date] >= a.[Starting Date]
AND b.[Invoiced Quantity] <>0
order by b.[Posting Date]

0 个答案:

没有答案
相关问题