JOIN两个表但只返回表1中与表2匹配的行

时间:2016-06-28 11:31:54

标签: sql sql-server join

美好的一天 我有两个表需要加入,Transfer Excise Tbl和Value Entry

转移消费税:否必须与“值输入”表中的“项目编号”匹配。我确实做了一个比较不在转移消费税中的物品在价值条目中,并找到了一些。

转移消费税:

Starting Date           No_                  Excise Location Location Code Unit Rate                               Excise Type Code Unit Of Measure Code Litre Conversion Factor
----------------------- -------------------- --------------- ------------- --------------------------------------- ---------------- -------------------- ---------------------------------------
2013-02-28 00:00:00.000 600011263            NONBOND         ~DUTY PAID    2.70000000000000000000                  UWNEPACK         LITRES               1.33333000000000000000  
2014-02-27 00:00:00.000 600011263            NONBOND         ~DUTY PAID    2.87000000000000000000                  UWNEPACK         LITRES               1.33333000000000000000  
2015-02-26 00:00:00.000 600011263            NONBOND         ~DUTY PAID    3.07000000000000000000                  UWNEPACK         LITRES               1.33333000000000000000  
2016-02-25 00:00:00.000 600011263            NONBOND         ~DUTY PAID    3.31000000000000000000                  UWNEPACK         LITRES               1.33333000000000000000

值输入表:

Item No_             Location Code Gen_ Bus_ Posting Group Invoiced Quantity
-------------------- ------------- ----------------------- ---------------------------------------
F00330               VINI          EXSA                    -10.00000000000000000000  
F00331               VINI          EXSA                    -30.00000000000000000000  
F00332               VINI          EXSA                    -40.00000000000000000000  

我想编写查询以排除重复项,因为下面的脚本仍会创建重复项。 PK为Item No,FK为Location Code。您将在转移消费税表中看到,每年我为特定物料和地点提供新的单位费率

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 JOIN [Spier Live$Value Entry] b
ON a.[No_] = b.[Item No_] 
WHERE  b.[Posting Date] > '2013-02-26 '
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

1 个答案:

答案 0 :(得分:0)

首先,您的[Value Entry]表有问题。

1)在您的查询中,您可以参考[过帐日期]列,但示例数据中没有此类列。

现在,如果我已经很好地理解了这个场景,我认为你的问题与你如何从两个表中加入线条有关。

我得到的线数超出预期,因为你在[值输入]中加入了[转移消费税]中的所有行,其中[开始日期]较旧,而不是最后(有效)行。

要解决此问题,您应预先计算[Transfer Excise Tbl]行的有效期,找到每行的[结束日期],然后您将 加入b。[发布日期] BETWEEN a。[开始日期] AND a。[结束日期]

最终查询将类似于:

;WITH
EndDates as (-- add [End Date] to [Transfer Excise Tbl]
    select t1.*, ISNULL([End Date], CONVERT(date, '9999-12-31', 121)) [End Date]
    from [Transfer Excise Tbl] t1
    outer apply (
        select MIN([Starting Date]) [End Date]
        from [Transfer Excise Tbl]
        where [Starting Date] > t1.[Starting Date]
    ) T2
)
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 [EndDates] a JOIN [Spier Live$Value Entry] b ON a.[No_] = b.[Item No_] AND b.[Posting Date] BETWEEN a.[Starting Date] AND a.[End Date]
WHERE  b.[Posting Date] > '2013-02-26 '
AND b.[Location Code] = a.[Location Code]
AND b.[Gen_ Bus_ Posting Group] IN ('LOCA','EXSA')    
AND b.[Invoiced Quantity] <> 0

它应该只返回您期望的行数

我希望这会有所帮助