select  isnull(SUM(netPaid),0.00) from dbo.F_AccountEntry a 
                                        right join  dbo.RPT_resultA41 b
                                        on a.CompanyID=b.CompanyID 
                                        and a.AcctID=b.acctid 
                                        and b.CompanyID=1 and b.userName='sally_law' 
                                        and a.FiscalDateID>-13 and a.FiscalDateID<0
                                        group by b.acctid如上语句,我的B表在and b.CompanyID=1 and b.userName='sally_law' 条件下 只有66条数据,
但这样出来的如果却有77条之多,什么原因呢,是我的语句有问题吗?

解决方案 »

  1.   

    left join 应该是66条数据吧。right join 会出来一些右表有,但是左表空的数据。right join 查询出来的结果中 左表空的也会算作一条数据。
      

  2.   

    right/left join都会添加“额外行”,你如果用inner join那应该就只有66行了,应该考虑是否有必要用外联,而不是随便用。
      

  3.   

    语句是没有问题的,只有理解的问题
    如果怀疑不对的,把条件写到where中去
      

  4.   

    但用inner join不行呢!因为我的A表中只有52条记录,inner join只能显示大家共有的,那满足不了要求,我想用B表有的全显示,A表没有的用null或0补充,不是应该用right join 吗?
      

  5.   

    where 都试过了,之前没有出现过这样的情况呢!有点奇怪!
      

  6.   


    就如图片所示的,查询出以A表acctid为组的B表的closingAmtBase的总和,
    最后得到的结果应该是acctid  closingAmtBase
                     81        9506.98
                     82         183207.21
                     83          0.00
                     84          0.00 
      

  7.   

    麻烦了,我这样有错?
    select SUM(closingAmtBase) ,b.acctid from tal a  right join kk  b 
    on   a.acctid=b.acctid and a.companyid=b.companyid 
    group by b.acctid
      

  8.   

    但如果却是.。。
    sum                   acctid
    --------------------- -----------
    19013.96              81
    366414.42             82
    NULL                  83
    NULL                  84
    Warning: Null value is eliminated by an aggregate or other SET operation.(4 row(s) affected)
      

  9.   

    猜你想要的没有意思(你汇总的字段是哪个表的我们都不知道),最好你自己搞清楚条件写在where和on的区别实在不行,你先不要汇总,而是修改为
    select *
    看看结果是不是你想要的结果,多了哪些,哪些重复了,修改条件得到你需要的再修改为汇总语句,这样才能保证结果与你预想一样(当然搞清楚逻辑也可以一次写对)
      

  10.   

    刚才有事去忙了----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-01-14 16:39:38
    -- Version:
    --      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64) 
    -- Jun 17 2011 00:54:03 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
    --
    ----------------------------------------------------------------
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    go 
    create table [A]([companyid] int,[acctid] int)
    insert [A]
    select 1,81 union all
    select 1,82 union all
    select 1,83 union all
    select 1,84if object_id('[b]') is not null drop table [b]
    go 
    create table [b]([companyid] int,[fiscaldateid] int,[acctid] int,[closingamtbase] numeric(8,2))
    insert [b]
    select 1,1,81,9506.98 union all
    select 1,1,82,148151.56 union all
    select 1,2,81,0.00 union all
    select 1,2,82,11585.23 union all
    select 1,3,81,0.00 union all
    select 1,3,82,23470.42
    --------------开始查询--------------------------SELECT a.acctid,SUM(ISNULL(closingamtbase,0))closingamtbase
    FROM a LEFT JOIN b ON a.companyid=b.companyid AND a.acctid=b.acctid
    GROUP BY a.acctid/*
    acctid      closingamtbase
    ----------- ---------------------------------------
    81          9506.98
    82          183207.21
    83          0.00
    84          0.00(4 行受影响)
    */
      

  11.   

    谢谢黄老师了
    这不是和我的一样吗?就是两个表的位置互換了,改成rihgt join,为什么那样就出问题了?
      

  12.   

    你在对比一下,怎么我改成right join就没问题?结果还是一样----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2013-01-14 16:39:38
    -- Version:
    --      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64) 
    -- Jun 17 2011 00:54:03 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
    --
    ----------------------------------------------------------------
    --> 测试数据:[A]
    if object_id('[A]') is not null drop table [A]
    go 
    create table [A]([companyid] int,[acctid] int)
    insert [A]
    select 1,81 union all
    select 1,82 union all
    select 1,83 union all
    select 1,84if object_id('[b]') is not null drop table [b]
    go 
    create table [b]([companyid] int,[fiscaldateid] int,[acctid] int,[closingamtbase] numeric(8,2))
    insert [b]
    select 1,1,81,9506.98 union all
    select 1,1,82,148151.56 union all
    select 1,2,81,0.00 union all
    select 1,2,82,11585.23 union all
    select 1,3,81,0.00 union all
    select 1,3,82,23470.42
    --------------开始查询--------------------------SELECT a.acctid,SUM(ISNULL(closingamtbase,0))closingamtbase
    FROM b right JOIN a ON a.companyid=b.companyid AND a.acctid=b.acctid
    GROUP BY a.acctid/*
    acctid      closingamtbase
    ----------- ---------------------------------------
    81          9506.98
    82          183207.21
    83          0.00
    84          0.00(4 行受影响)
    */
      

  13.   


    SELECT a.acctid,isnull(bb.closingAmtBase,0) closingAmtBase FROM a LEFT JOIN  (
    SELECT SUM(closingAmtBase) closingAmtBase ,acctid,companyid FROM b
    GROUP BY acctid,companyid
    ) bb  ON bb.companyid = a.companyid AND bb.acctid = a.acctid81 9506.98
    82 183207.21
    83 0.00
    84 0.00
      

  14.   

    难道是人品问题我写的跟黄老师的一样的,left join 也一样,案还是出错了,唉!
      

  15.   

    acctid      closingamtbase
    ----------- ---------------------
    81          19013.96
    82          366414.42
    83          0.00
    84          0.00(4 row(s) affected)看来我的是神了
      

  16.   

    你看看是不是acctid写错了表名
      

  17.   

    此话是正解,还有,
    right join 完全造价于left join,你左右一换,比对一番,即可找出相异之处。