我想基於一個列(sysprocode),這是複合關鍵字(Organizationunitid和payrollcodeid)唯一的獲得金額的總和。到目前爲止我已經設法得到基於Organizationunitid和payrollcodeid的金額但不是靠sysprocode.A看我SQLfiddle應該澄清這更Click 在這裏,我想這SQL查詢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
你想要什麼輸出,在你的問題中增加樣本輸出數據 – 2014-12-02 06:50:23
done @SaharshShah – GotaloveCode 2014-12-02 07:00:43