我有这个代码可以返回我想要的结果;
select stktype, Part_No,
Case when stktype = 'labour' then Part_No END
from fleet_job_jobdetails
where JobCode = '176071' AND StkType = 'labour'
group by part_no, StkType
stktype Part_No(无列名) 劳工ANDR ANDR
当我将其添加到我的较大代码时,我收到错误
LEFT JOIN (
SELECT jobcode
, SUM(linecost) AS totalcost
, SUM(CASE WHEN stktype = 'sublet' THEN linecost
ELSE 0
END) AS subletcost
, SUM(CASE WHEN stktype = 'labour' THEN linecost
ELSE 0
END) AS labourcost
, SUM(CASE WHEN stktype = 'part' THEN linecost
ELSE 0
END) AS partscost
, SUM(CASE WHEN stktype NOT IN ('sublet', 'labour', 'part') THEN linecost
ELSE 0
END) AS othercost
, **CASE WHEN (select StkType from fleet_job_jobdetails WHERE stktype = 'labour') = 'labour' THEN Part_No ELSE '' END AS Mech**
FROM fleet_job_jobdetails
GROUP BY jobcode, Part_No
) a
ON fleet_job_jobmaster.jobcode = a.jobcode
答案 0 :(得分:0)
Part_No 仅在这种情况下
** CASE
WHEN (select StkType
from fleet_job_jobdetails
WHERE stktype = 'labour') = 'labour' THEN
Part_No
因此没有Part_No情况,您必须修改语句以确保 Part_No 存在而不是WHEN THEN
或您只能按jobcode
答案 1 :(得分:0)
尝试:
LEFT JOIN (
SELECT jobcode
, SUM(linecost) AS totalcost
, SUM(CASE WHEN stktype = 'sublet' THEN linecost
ELSE 0
END) AS subletcost
, SUM(CASE WHEN stktype = 'labour' THEN linecost
ELSE 0
END) AS labourcost
, SUM(CASE WHEN stktype = 'part' THEN linecost
ELSE 0
END) AS partscost
, SUM(CASE WHEN stktype NOT IN ('sublet', 'labour', 'part') THEN linecost
ELSE 0
END) AS othercost
--Here seems subquery is not required we can simply replace with it
, CASE WHEN stktype = 'labour' THEN Part_No ELSE '' END AS Mech
FROM fleet_job_jobdetails
GROUP BY jobcode, Part_No
) a
ON fleet_job_jobmaster.jobcode = a.jobcode