在SQL语句中,我需要使用三种不同的条件查询同一列三次。 我需要为所有地点显示CC.HR1 * WLL17.Hours的SUM,但有三种不同的条件。我在SELECT语句中尝试了一个子查询,但后来我只得到总和而不是按位置分组的总和。
SELECT WLL17.Location, (SELECT SUM(CC.HR1*WLL17.Hours) FROM WLL17 INNER JOIN CC ON WLL17.CC=CC.CC WHERE WLL17.EE = "Post Gate 60 (PV)") AS Plant
FROM WLL17 INNER JOIN CC ON WLL17.CC = CC.CC
WHERE WLL17.CC<>"Outsourced Costs"
GROUP BY WLL17.Location;
答案 0 :(得分:1)
尝试遵循此策略。
SELECT WLL17.Location,
SUM(case when WLL17.EE = "Post Gate 60 (PV)" then CC.HR1*WLL17.Hours else null end) AS Plant,
SUM(case when WLL17.CC<>"Outsourced Costs" then CC.HR1*WLL17.Hours else null end) AS Location_Name1,
SUM(case when [Write condition here] then CC.HR1*WLL17.Hours else null end) AS Location_Name2
FROM WLL17 INNER JOIN CC ON WLL17.CC = CC.CC
GROUP BY WLL17.Location;