SQL外连接返回太多

时间:2013-11-19 12:49:46

标签: sql sql-server-2008

我有两张桌子:

create table custinvoicetrans
      ( LINEAMOUNTMST numeric(28,12)
      , INVENTTRANSID nvarchar(20)
      , DATAAREAID nvarchar (4)
      )
;
insert into custinvoicetrans (LINEAMOUNTMST, INVENTTRANSID, DATAAREAID) VALUES
(2685.850000000000, N'00006840_069', N'dk-l'), 
(6162.220000000000, N'00034511_069', N'DK-L'), 
(-1453.350000000000, N'00035042_069', N'DK-L')
;
create table INVENTTRANS
      ( CUSTVENDAC nvarchar(20)
      , ItemId nvarchar(20)
      , QTY numeric(28,12)
      , COSTAMOUNTPOSTED numeric(28,12)
      , COSTAMOUNTPHYSICAL numeric(28,12)
      , COSTAMOUNTADJUSTMENT numeric(28,12)
      , DATAAREAID nvarchar(20)
      , INVENTTRANSID nvarchar(20)
      )
;
insert into INVENTTRANS (CUSTVENDAC, ItemId, QTY, COSTAMOUNTPOSTED, COSTAMOUNTPHYSICAL, COSTAMOUNTADJUSTMENT, DATAAREAID, INVENTTRANSID) VALUES
(N'100251', N'F20100003', -120.000000000000, 0.000000000000, 0.000000000000, -1034.630000000000, N'dk-l', N'00006840_069'), 
(N'100251', N'F20301000', -50.000000000000, -664.500000000000, -664.500000000000, 0.000000000000, N'DK-L', N'00034511_069'), 
(N'100251', N'F20301000', -162.000000000000, -2152.980000000000, -2152.980000000000, 0.000000000000, N'DK-L', N'00034511_069'), 
(N'100251', N'F20301000', 50.000000000000, 664.500000000000, 664.500000000000, 0.000000000000, N'DK-L', N'00035042_069')
;

当我加入如下表格时,从custinvoicetrans获取值时,我获得项目'20301000'的双值。 我不知道该怎么做 - 我花了几个小时和几个小时以各种不同的方式解决它,但我无法获得正确的金额。

请帮帮我。

我可以看到出了什么问题,但我找不到解决方案。

马丁

    select CUSTVENDAC
      , ITEMID
      , sum(QTY) QTY
      , sum(LINEAMOUNTMST) Revenue
      , sum(LINEAMOUNTMST) + sum(COSTAMOUNTADJUSTMENT) + sum(COSTAMOUNTPHYSICAL) Margin
from INVENTTRANS
left outer join CUSTINVOICETRANS on INVENTTRANS.INVENTTRANSID = CUSTINVOICETRANS.INVENTTRANSID
                                    and INVENTTRANS.DATAAREAID = CUSTINVOICETRANS.DATAAREAID
where INVENTTRANS.DATAAREAID = 'dk-l'
      and INVENTTRANS.CUSTVENDAC = '100251'
group by INVENTTRANS.CUSTVENDAC
      , INVENTTRANS.ITEMID
order by INVENTTRANS.CUSTVENDAC
      , INVENTTRANS.ITEMID

2 个答案:

答案 0 :(得分:0)

你应该注意在On中使用谓词。

  

“了解外部连接,ON和。非常重要   WHERE条款扮演着截然不同的角色,因此,它们不是   互换。 WHERE子句仍然可以进行简单的过滤   角色 - 即保持真实案件并丢弃虚假和未知案件。   使用类似的东西并在where子句中使用谓词。然而,   ON子句不起简单的过滤作用;相反,它更多   匹配的角色。换句话说,保留一侧的行将是   返回ON谓词是否找到匹配。所以   ON谓词仅确定来自非保留方的哪些行   与保留边的行匹配 - 不是是否返回行   从保留的一面。“**考试70-461:查询Microsoft SQL Server   2012

答案 1 :(得分:0)

您可以为INVENTRANS表创建子查询

 select CUSTVENDAC
          , ITEMID
          , sum(QTY) QTY
          , sum(LINEAMOUNTMST) Revenue
          , sum(LINEAMOUNTMST) + sum(COSTAMOUNTADJUSTMENT) + sum(COSTAMOUNTPHYSICAL) Margin
    from (
        Select 
        CUSTVENDAC,
        ITEMID, 
        sum(QTY) 'QTY',
        sum(COSTAMOUNTPOSTED) 'COSTAMOUNTPOSTED',
        sum(COSTAMOUNTPHYSICAL) 'COSTAMOUNTPHYSICAL',
        sum(COSTAMOUNTADJUSTMENT) 'COSTAMOUNTADJUSTMENT',
        DATAAREAID,
        INVENTTRANSID
    from INVENTTRANS
    group by 
        CUSTVENDAC,
        ITEMID, 
        DATAAREAID,
        INVENTTRANSID
    )INVENTTRANS
    left outer join #CUSTINVOICETRANS CUSTINVOICETRANS on INVENTTRANS.INVENTTRANSID =          CUSTINVOICETRANS.INVENTTRANSID
                                    and INVENTTRANS.DATAAREAID =     CUSTINVOICETRANS.DATAAREAID
where INVENTTRANS.DATAAREAID = 'dk-l'
      and INVENTTRANS.CUSTVENDAC = '100251'
group by INVENTTRANS.CUSTVENDAC
      , INVENTTRANS.ITEMID
order by INVENTTRANS.CUSTVENDAC
      , INVENTTRANS.ITEMID