There are many ways to do this but relying on the WOGroup avoids having to bring the WOAncestor into it. Below are 2 options:
SELECT WO.WONum, WO.WorkType,
(SELECT SUM(ActLabCost) FROM WorkOrder AC WHERE AC.WOGroup = WO.WOGroup) as "Actual Labor Cost",
(SELECT SUM(ActMatCost) FROM WorkOrder AC WHERE AC.WOGroup = WO.WOGroup) as "Actual Material Cost"
FROM WorkOrder WO
WHERE WO.IsTask = 0
ORDER BY WO.WONum
;
This option avoids having to use a GROUP BY but the multiple embedded SELECTs may not perform as well as the next option.
SELECT WO.WONum, MIN(WO.WorkType) as "Work Type",
SUM(AC.ActLabCost) as "Actual Labor Cost",
SUM(AC.ActMatCost) as "Actual Material Cost"
FROM WorkOrder WO
INNER JOIN WorkOrder AC
ON AC.WOGroup = WO.WOGroup
WHERE WO.IsTask = 0
GROUP BY WO.WONum
ORDER BY WO.WONum
;
This option relies on a JOIN between the Non-Task WorkOrder records and all WorkOrder records with the same WOGroup including the Non-Task WO.
Using the "MIN(WO.WorkType)" avoids having to include every field you want to include from the Non-Task WO in the GROUP BY clause.but it may be a bit harder to read. Just using "WO.WorkType" in the main SELECT and repeating all the "WO.Xxx..." columns in the GROUP BY clause accomplishes the same thing.
------------------------------
Julio Hernandez
------------------------------