First off this is a project management database I'm messing with. So I have 3 things I want to JOIN
together a planning_entity
and two subqueries. These subqueries get apart of a tree structure. The parts of the tree structure I want are Project, Phase, and Activity. I need the names of the Project, Phase, The Start and Finish date of the Phase, and the hours devoted to them. The problem is, is that the hours are connected with the Activity, and the dates are the Phase. When I JOIN
these together it pulls up the end dates for the Activity when i want it to pull up NULL
for the Phase because the Phase has not yet been finished.
Example Code :
SELECT MIN(spp.Project) AS 'Project', spp2.Phase
, FORMAT(MIN(stp.actual_start), 'd', 'en-US') AS 'Actual Start', FORMAT(MIN(stp.actual_finish), 'd', 'en-US') AS 'Actual Finish'
, FORMAT(MAX(effort_actual) + MAX(effort_reserved) + MAX(effort_required) + MAX(effort_scheduled) , 'N', 'en-us') AS 'Forecast Effort'
, FORMAT((MAX(baseline_allocated_effort) + MAX(baseline_reserved_effort) + MAX(baseline_required_effort)) , 'N', 'en-us') AS 'Baseline Effort'
, FORMAT(MAX(effort_actual), 'N', 'en-us') AS 'Effort Actual'
FROM ip.planning_entity stp
LEFT JOIN (SELECT ips.father_code
, (SELECT ips2.description
FROM ip.structure ips2
WHERE depth = 5 AND ips2.structure_code = ips.father_code) AS 'Project'
, ips.description AS 'Activity'
FROM ip.structure ips
WHERE ips.depth = 6) AS spp ON spp.father_code = stp.ppl_code
LEFT JOIN (SELECT ips.structure_code
, (SELECT ips2.description
FROM ip.structure ips2
WHERE depth = 6 AND ips2.structure_code = ips.father_code) AS 'Phase'
, ips.description AS 'Activity'
FROM ip.structure ips
WHERE ips.depth = 7) AS spp2 ON spp2.structure_code = stp.planning_code
LEFT JOIN ip.vw_ppm_cube_fact f on f.ppl_code = stp.ppl_code
WHERE stp.ppl_code in
(SELECT structure_code FROM ip.portal_filter WHERE filter_code = 83686 and usage='EX')
GROUP BY spp2.Phase
This is currently getting me the end dates from the Activity and not the Phase, any suggestions?
Sample data
PLANNING_ENTITY
planning_code actual_start actual_finish ppl_code
75962 2015-03-02 08:00:00.000 NULL 75962
82469 2015-04-30 08:00:00.000 NULL 82469
82629 2015-05-20 08:00:00.000 NULL 82629
82825 NULL NULL 82825
82854 2015-05-08 08:00:00.000 NULL 82854
82914 2015-05-11 08:00:00.000 NULL 82914
82943 2015-06-05 08:00:00.000 NULL 82943
82972 2015-04-09 08:00:00.000 NULL 82972
83001 2015-06-02 08:00:00.000 NULL 83001
83030 2015-03-27 17:00:00.000 2015-04-20 21:55:00.000 83030
83053 2015-04-20 08:00:00.000 NULL 83053
83085 NULL NULL 83085
83114 NULL NULL 83114
83192 NULL NULL 83192
83222 NULL NULL 83222
83251 NULL NULL 83251
83279 NULL NULL 83279
83336 NULL NULL 83336
83368 NULL NULL 83368
83427 NULL NULL 83427
83456 NULL NULL 83456
83489 NULL NULL 83489
83518 NULL NULL 83518
85658 NULL NULL 85658