I have this code which returns the result I want;
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 (No column name) Labour ANDR ANDR
when I add it to my larger code i get error
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
Part_No only in this case
** CASE
WHEN (select StkType
from fleet_job_jobdetails
WHERE stktype = 'labour') = 'labour' THEN
Part_No
so there will be no Part_No situation,you must revise the statement to make sure the Part_No exist and not in WHEN THEN
OR you just only group by jobcode
TRY THIS:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.