0
這3個查詢怎麼可以加入到1中。我該如何加入3個SQL查詢
我不是在尋找UNION ALL結果。
每排我想預測,預算,實際
SELECT
ps.PersonId,
SUM(TF.Hours * r.Amount) as Forecast
FROM ProjectStaffing ps
INNER JOIN Rate r on (r.ProviderId = ps.ProviderId AND r.OrganizationId = ps.OrganizationId AND r.RoleId = ps.RoleId)
INNER JOIN TimeForecast TF on (TF.ProjectStaffingId = ps.Id)
WHERE ps.ProjectId = @ProjectId
GROUP BY ps.PersonId
SELECT
ps.PersonId,
SUM(tb.Hours * r.Amount) as Budget
FROM ProjectStaffing ps
INNER JOIN Rate r on (r.ProviderId = ps.ProviderId AND r.OrganizationId = ps.OrganizationId AND r.RoleId = ps.RoleId)
INNER JOIN TimeBudget tb on (tb.ProjectStaffingId = ps.Id)
WHERE ps.ProjectId = @ProjectId
GROUP BY ps.PersonId
SELECT
ps.PersonId,
SUM(ta.Hours * r.Amount) as Actual
FROM ProjectStaffing ps
INNER JOIN Rate r on (r.ProviderId = ps.ProviderId AND r.OrganizationId = ps.OrganizationId AND r.RoleId = ps.RoleId)
INNER JOIN TimeActual ta on (ta.ProjectStaffingId = ps.Id)
WHERE ps.ProjectId = @ProjectId
GROUP BY ps.PersonId
我嘗試這樣做了,但只是得到奇怪的結果
SELECT
PS.PersonId,
SUM(TF.Hours * r.Amount) as Forecast,
SUM(TB.Hours * r.Amount) as Budget,
SUM(TA.Hours * r.Amount) as Actual
FROM ProjectStaffing PS
INNER JOIN Rate r on (r.ProviderId = PS.ProviderId AND r.OrganizationId = PS.OrganizationId AND r.RoleId = PS.RoleId)
LEFT JOIN TimeForecast TF on (TF.ProjectStaffingId = PS.Id)
LEFT JOIN TimeBudget TB on (TB.ProjectStaffingId = PS.Id)
LEFT JOIN TimeActual TA on (TA.ProjectStaffingId = PS.Id)
WHERE PS.ProjectId = @ProjectId
AND TF.Hours is not null
GROUP BY PS.PersonId
刪除'和TF.Hours不null'或'更換和(TF.Hours不爲空或TB.Hours不爲空或TA.Hours不爲null)' –
你是什麼意思奇怪的結果?你能把樣本結果添加到問題中嗎? – Susilo