如何联接2个表,以便您可以正确地对两个表中的字段求和

时间:2019-06-11 20:30:25

标签: sql join sum

我要加入2个表并求和3个字段,其中2个来自1个表,1个来自第二个表。但是,这些表有多个记录,并且给了我不正确的总计(总和)。我知道错在哪里,但不知道如何编写SQL以获得正确的结果。

由于CCNum 001的2条记录,因此总计基本上翻了一番。 所以很明显,SQL是不正确的,但是正确的SQL是什么?

不知道SQL是什么样子。

SELECT  AETCC.PayPeriodID, EI.ADP_ID, EI.LastName + ', ' 
 + EI.FirstName AS EmpName, AETCC.LocationNum 
 + '-' + AETCC.CostCenterNum AS CostString, 
 AETCC.LocationNum AS Location, AETCC.CostCenterNum AS CostCenter, 
 AllocationType, sum(AD.TotalHoursWorked) as TotalHoursWorked, 
 sum(AD.TotalPercentWorked) as TotalPercentWorked, 
 sum(AETCC.ExpenseAmount) AS ExpAmt
        FROM    dbo.AllocatedExpenseToCostCenter AS AETCC 
        INNER JOIN
            dbo.AllocationData AS AD 
                ON AETCC.ADP_ID = AD.ADP_ID 
                AND AETCC.CostCenterNum = AD.CostCenterNum 
                AND AETCC.LocationNum = AD.LocationNum 
                AND AETCC.PayPeriodID = AD.PayPeriodID 
        INNER JOIN
            dbo.EmployeeInformation AS EI 
                ON AETCC.ADP_ID = EI.ADP_ID
        WHERE        (AETCC.ExpenseAccount = '8000')
--      GROUP BY AETCC.PayPeriodID, EI.ADP_ID, EI.FirstName, EI.LastName,
--      AETCC.LocationNum, AETCC.CostCenterNum
        GROUP BY AETCC.PayPeriodID, AllocationType, AETCC.LocationNum,
        AETCC.CostCenterNum, EI.ADP_ID, EI.FirstName, EI.LastName

第一个表中的数据:

PayPrd,LocNum,CCNum,ExpAcct,ExpenseAmount,ADP_ID,HoursWorked
1921,024,001,8000,175.2496,2063,0.63
1921,024,001,8000,1810.2617,2063,67.49
1921,024,013,8000,93.3417,2063,0.18

第二个表中的数据:

PayPrd,LocNum,CCNum,Hours,%,ADP_ID
1921,024,001,7.50,0.0843,2063
1921,024,001,77.50,87.0787,2063
1921,024,013,4.00,0.0449,2063

当前结果:

PayPrd,ADP_ID,EmpName,CostString,LocNum,CCNum,Hours,%,ExpAmt
1921,2063,King; Tom,024-001,024,001,170.00,174,3971.0226
1921,2063,King; Tom,024-013,024,013,4.00,0.4,93.3417

1 个答案:

答案 0 :(得分:1)

每个表的总和,然后将结果合并。

SELECT  AETCC.PayPeriodID, EI.ADP_ID, EI.LastName + ', ' 
 + EI.FirstName AS EmpName, AETCC.LocationNum 
 + '-' + AETCC.CostCenterNum AS CostString, 
 AETCC.LocationNum AS Location, AETCC.CostCenterNum AS CostCenter, 
 AllocationType, AD.TotalHoursWorked as TotalHoursWorked, 
 AD.TotalPercentWorked as TotalPercentWorked, 
 AETCC.ExpenseAmount AS ExpAmt
      FROM (SELECT PayPeriodID, LocationNum, CostCenterNum, ADP_ID, ExpenseAccount,
              SUM(ExpenseAmount) as ExpenseAmount
            FROM dbo.AllocatedExpenseToCostCenter
            GROUP BY PayPeriodID, LocationNum, CostCenterNum, ADP_ID, ExpenseAccount
           ) AS AETCC 
        INNER JOIN
           (SELECT PayPeriodID, LocationNum, CostCenterNum, ADP_ID,
              SUM(TotalHoursWorked) as TotalHoursWorked,
              SUM(TotalPercentWorked) as TotalPercentWorked
            FROM dbo.AllocationData
            GROUP BY PayPeriodID, LocationNum, CostCenterNum, ADP_ID
           ) AS AD
                ON AETCC.ADP_ID = AD.ADP_ID 
                AND AETCC.CostCenterNum = AD.CostCenterNum 
                AND AETCC.LocationNum = AD.LocationNum 
                AND AETCC.PayPeriodID = AD.PayPeriodID 
        INNER JOIN
            dbo.EmployeeInformation AS EI 
                ON AETCC.ADP_ID = EI.ADP_ID
        WHERE        (AETCC.ExpenseAccount = '8000')

您还可以在AETCC子查询中移动WHERE子句。