我正在尝试合并两个表,但我的结果显示一个表中的数据不正确。
一个例子是我的第一张表有以下内容:
PositionCode AcctUnit BudgetFTE
JDFF HRT 2
我的第二个表不包含PositionCode = JDFF和AcctUnit = HRT的任何记录,但是通过运行以下查询得到以下结果:
PositionCode AcctUnit BudgetFTE ActualFTE VarianceFTE
JDFF HRT 2 1 -1
sql代码:
SELECT
b.PositionCode,
b.AcctUnit,
e.POSITION,
e.HM_ACCT_UNIT,
b.FTE AS BudgetFTE,
sum(e.FTE) AS ActualFTE,
sum(e.FTE) - b.FTE AS VarianceFTE
FROM
QryBudgetRollUp AS b
LEFT JOIN ActiveEmployees AS e ON
( b.PositionCode = e.POSITION ) AND
( e.HM_ACCT_UNIT = b.AcctUnit )
GROUP BY
b.PositionCode,
b.AcctUnit,
e.POSITION,
e.HM_ACCT_UNIT,
b.FTE,
e.FTE - b.FTE
示例数据:
QryBudgetRollup
PositionCode AcctUnit FTE
JDFF HRT 2
VIPP HRT 1
HROPSA CMP 1
ActiveEmployees
C E CE LAST_NAME FIRST_NAME EMP PROCESS HM_ACCT_UNIT D POSITION D;P' Date SC Expr1013 AP PG HC FTE PE
2 2343 22343 Doe John FT CHRE CMP CMP HROPSA CMP;HROPSA 2/15/1999 H $20.00 $40,000.00 4 1 1 $4,000.00
2 2515 22515 Jetson George PT CHRE CMP CMP HROPSA CMP;HROPSA 4/22/2014 H $10.00 $20,000.00 2 1 0.5 $2,000.00
4 18 418 Doe Jane FT CSIS HRT HRT VIPP HRT;VIPP 11/1/2002 S $40.00 $80,000.00 7 1 1 $8,000.00
所需的查询结果
PositionCode AcctUnit BudgetFTE ActualFTE VarianceFTE
JDFF HRT 2 0 2
VIPP HRT 1 1 0
HROPSA CMP 1.5 2 -0.5