sql sum()导致左外连接加倍值

时间:2015-08-13 19:35:14

标签: sql sql-server

dbo.SalespersonProject上的左对齐连接导致TranAmt的值加倍。我需要对项目的合法TranAmt求和,但是在左外连接和SalesPerson02中添加会导致匹配不正确的加倍。

SELECT ARDoc.SlsperId AR_Doc_Sls_ID
     , CASE
       WHEN ARTran.TranType = 'CM' THEN SUM(ARTran.TranAmt) * -1
           ELSE SUM(ARTran.TranAmt) 
       END TranAmt
     , MAX(CASE
           WHEN SalesCommOrder = 1 THEN SalesPersonId
           END) Salesperson01
     , MAX(ISNULL(CASE
                  WHEN SalesCommOrder = 1 THEN Percentage
                  END, .03)) Commission01
     , MAX(CASE
           WHEN SalesCommOrder = 2 THEN SalesPersonId
           END) Salesperson02
     , MAX(CASE
           WHEN SalesCommOrder = 2 THEN Percentage
           END) Commission02
     , PJPROJ.project PJ_ID
     , PJPROJ.project_desc PJ_Description
     , PJPROJ.slsperid SME
     , ARDoc.CustId Cust_ID
     , CASE
       WHEN RTRIM(ARTran.InvtId) = 'GRAPHICS' THEN 1
       WHEN RTRIM(ARTran.InvtID) = 'SERVICE CONTRACT' THEN 2
       END InvtID
     , RTRIM(ARDoc.CustId) + ' ' + Customer.BillName Cust_ID_Name
     , CONVERT( DATE, ARTran.TranDate, 101) Doc_Date
     , ARTran.TranType Doc_Type
     , ARTran.RefNbr
     , PJPROJ.start_date
     , SUM(ARTran.ExtCost) ExtCost
     , SUM(ARTran.UnitPrice) UnitPrice
     , SUM(ARTran.Qty) Qty
     , (
        SELECT SUM(b.eac_amount) [Budg Rev]
               FROM pjptdsum b
                    INNER JOIN pjacct a ON a.acct = b.acct
                                       AND a.acct_type = 'RV'
               WHERE ARDoc.ProjectID = b.project) Budget_Rev
     , (
        SELECT SUM(b.eac_amount) [Budg Rev]
               FROM pjptdsum b
                    INNER JOIN pjacct a ON a.acct = b.acct
                                       AND a.acct_type = 'EX'
               WHERE ARDoc.ProjectID = b.project) Budget_EAC
     , so.TotMerch SalesOrderTotal
     , Salesperson.Name
     , ARTran.PerPost
     , PJPROJ.manager2
     , PJEMPLOY.emp_name
       FROM Testnewgroundapp.dbo.ARDoc ARDoc
            INNER JOIN Testnewgroundapp.dbo.ARTran ARTran ON ARDoc.CustId = ARTran.CustId
                                                         AND ARDoc.RefNbr = ARTran.RefNbr
                                                         AND ARDoc.DocType = ARTran.TranType
            INNER JOIN Testnewgroundapp.dbo.Customer Customer ON ARDoc.CustId = Customer.CustId --INNER JOIN @CustomerTab ct

            LEFT OUTER JOIN Testnewgroundapp.dbo.Salesperson Salesperson ON ARDoc.SlsperId = Salesperson.SlsperId
            LEFT OUTER JOIN Testnewgroundapp.dbo.PJPROJ PJPROJ ON ARDoc.ProjectID = PJPROJ.project
            LEFT OUTER JOIN Testnewgroundapp.dbo.PJEMPLOY PJEMPLOY ON PJPROJ.manager2 = PJEMPLOY.employee
            LEFT OUTER JOIN (
                             SELECT h.PerPost
                                  , h.SlsperID
                                  , SUM(h.TotMerch) TotMerch
                                    FROM SOShipHeader H
                                    GROUP BY h.PerPost
                                           , h.SlsperID) so ON ARTran.PerPost = so.PerPost
                                                           AND ARDoc.SlsperId = so.SlsperID
            LEFT OUTER JOIN TestCommissions.dbo.SalespersonProject SalespersonProject ON SalespersonProject.ProjectId = PJPROJ.project --AND SalespersonProject.SalesPerson_Id = @ApplicationUserID
            LEFT OUTER JOIN TestCommissions.dbo.SalesPerson AppSalesPerson ON AppSalesPerson.Id = SalespersonProject.SalesPerson_Id
       WHERE
             (ARTran.TranType = 'CM'
          AND ARTran.DrCr = 'D'
           OR ARTran.DrCr = 'C'
          AND
              (ARTran.TranType = 'CS'
            OR ARTran.TranType = 'DM'
            OR ARTran.TranType = 'IN'
              ) 
             ) 
         AND ARTran.TaskID NOT LIKE '%60850'
         AND ARTRan.TaskID NOT LIKE '%60900'
         AND ARTran.invtid <> 'TSCINC001'
         AND ARTran.TranClass NOT IN ('F', 'N', 'T') 
         AND ARTran.Acct NOT IN ('2590', '2040', '2037') 
         AND CONVERT(INT, ARTran.Acct) > 1301
         AND CONVERT(INT, ARTran.PerPost) BETWEEN 201504 AND 201504
         AND ARTran.Rlsed = 1
         AND PJPROJ.project IS NOT NULL
         AND artran.cpnyid IN ('R', 'A') 

       GROUP BY ARDoc.SlsperId

              , ARDoc.ProjectID
              , PJPROJ.project
              , PJPROJ.project_desc
              , PJPROJ.slsperid
              , ARDoc.CustId
              , ARTran.InvtId
              , Customer.BillName
              , ARTran.TranDate
              , ARTran.TranType
              , ARTran.RefNbr
              , PJPROJ.start_date
              , so.TotMerch
              , Salesperson.Name
              , ARTran.PerPost
              , ARDoc.CpnyID
              , PJPROJ.manager2
              , PJEMPLOY.emp_name

       HAVING ARDoc.SlsperId = 'bpettit'
       ORDER BY ARDoc.SlsperId, ARTran.PerPost, PJPROJ.project

1 个答案:

答案 0 :(得分:0)

如果额外连接表每个主记录有两个记录,这会使每个主记录在结果集中出现两次。然后,以下分组和求和将对主记录或另一个连接表的每个值求和两次。

暂时删除分组和求和,但保留所有连接并查看结果集。然后你可能会看到导致这种倍增的原因。