我正在尝试获取基于列(sysprocode)的金额总和,该列对于组合键(Organizationunitid和payrollcodeid)是唯一的。到目前为止,我已经设法根据Organizationunitid和payrollcodeid获取金额,但不是在sysprocode.A旁边。看看我的 SQLfiddle 应该更多地阐明这一点Click 这里我试过这个SQL查询
SELECT TB1.OUId,TB1.OUName,(TB2.Amount - TB3.ManualDeduction) AS amt
FROM
(
SELECT ou.OrganizationUnitID AS OUId,ou.OrganizationUnitName AS OUName
FROM OrganizationUnits ou
) TB1,
(SELECT e.OrganizationUnitID AS OUId,SUM(trn.Amount) AS Amount
FROM Employees e
LEFT JOIN tblPeriodTransactions trn ON (e.EmployeeID=trn.Employee_ID)
where trn.Period_Month =6 and trn.Period_Year=2013 and trn.PayrollCode_ID=2
GROUP BY e.OrganizationUnitID
)TB2,
(SELECT e.OrganizationUnitID AS OUId,SUM(ep.ManualDeduction) AS ManualDeduction
FROM Employees e
LEFT JOIN tblEmployeePension ep ON (e.EmployeeID=ep.Employee_ID)
GROUP BY e.OrganizationUnitID
)TB3
WHERE (TB2.OUId=TB1.OUId)
AND (TB3.OUId=TB1.OUId)
以下是imgur sample output
中的示例输出答案 0 :(得分:0)
检查以下查询是否有效,但我认为计算养老金是有问题的,因为tblEmployeePension中没有PayrollCode_ID:
SELECT
OU.OrganizationUnitID,
OU.OrganizationUnitName,
SPC.sysprocode,
PC.PayrollCode_ID,
SUM(PD.Amount),
(SELECT SUM(tblEmployeePension.ManualDeduction) FROM tblEmployeePension WHERE Employee_ID IN (SELECT Employee_ID FROM Employees WHERE OrganizationUnitID = OU.OrganizationUnitID)) AS Pension,
SUM(PD.Amount) - (SELECT SUM(tblEmployeePension.ManualDeduction) FROM tblEmployeePension WHERE Employee_ID IN (SELECT Employee_ID FROM Employees WHERE OrganizationUnitID = OU.OrganizationUnitID)) as amt
FROM
tblPeriodTransactions PD
INNER JOIN
Employees E
ON
PD.Employee_ID = E.EmployeeID
INNER JOIN
OrganizationUnits OU
ON
E.OrganizationUnitID = OU.OrganizationUnitID
INNER JOIN
tblPayrollCode PC
ON
PD.PayrollCode_ID = PC.PayrollCode_ID
INNER JOIN
sysprocodes SPC
ON
SPC.organisationunitid = OU.OrganizationUnitID AND
SPC.PayrollCode_ID = PC.PayrollCode_ID
GROUP BY
OU.OrganizationUnitID,
PC.PayrollCode_ID,
SPC.sysprocode,
OU.OrganizationUnitName
答案 1 :(得分:0)
我在摸索后终于解决了这个问题
SELECT TB4.Syscode,
CASE TB4.accountType WHEN 'c' THEN concat('-', (TB2.Amount - TB3.ManualDeduction))
ELSE (TB2.Amount - TB3.ManualDeduction) end AS amount
FROM
(
SELECT ou.OrganizationUnitID AS OUId,ou.OrganizationUnitName AS OUName
FROM OrganizationUnits ou
) TB1,
(SELECT e.OrganizationUnitID AS OUId,SUM(trn.Amount) AS Amount
FROM Employees e
LEFT JOIN tblPeriodTransactions trn ON (e.EmployeeID=trn.Employee_ID)
WHERE trn.Period_Month = 6
AND trn.Period_Year = 2013
AND trn.PayrollCode_ID = 2
GROUP BY e.OrganizationUnitID
)TB2,
(SELECT e.OrganizationUnitID AS OUId,SUM(ep.ManualDeduction) AS ManualDeduction
FROM Employees e
LEFT JOIN tblEmployeePension ep ON (e.EmployeeID=ep.Employee_ID)
GROUP BY e.OrganizationUnitID
)TB3,
(SELECT ou.OrganizationUnitID AS OUId,sp.sysprocode as Syscode,sp.accountType AS accountType
FROM OrganizationUnits ou
INNER JOIN sysprocodes sp ON (ou.OrganizationUnitID=sp.organisationunitid)
INNER JOIN tblpayrollcode pc ON (pc.PayrollCode_ID = sp.PayrollCode_ID)
where sp.PayrollCode_ID = 2
GROUP BY ou.OrganizationUnitID,sp.sysprocode,sp.PayrollCode_ID,sp.accountType
)TB4
WHERE (TB2.OUId=TB1.OUId)
AND (TB3.OUId=TB1.OUId)
AND(TB4.OUId =tb3.OUId)
点击HERE查看 SQLFIDDLE