2009-10-20 6 views
2

현재 소프트웨어 프로젝트에 대한 보고서를 생성해야하는 경우가 있습니다. 두 개의 열은 최근 마일스톤의 날짜와 이전 마일스톤의 날짜입니다. 분명히 이정표는 트랜잭션 테이블에 저장되므로 프로젝트 당 많은 이정표를 가질 수 있습니다.단일 행으로 트랜잭션 선택

지금까지 여기에 왔지만, 지금은 데 문제 :

select proj.* 
from zsof_projects proj 
join zsof_milestones current_milestone on current_milestone.product_id = proj.id 
join zsof_milestones last_milestone on last_milestone.product_id = proj.id 
join (
     select product_id, max(actual_date) maxDate 
     from zsof_milestones 
     group by product_id 
     ) a on a.product_id = current_milestone.product_id and a.maxDate = current_milestone.actual_date 
join (
     select mile.product_id, max(actual_date) maxDate 
     from zsof_milestones mile 
     join (
       select product_id, max(actual_date) maxDate 
       from zsof_milestones 
       group by product_id 
       ) a on a.product_id = mile.product_id and mile.actual_date < a.maxDate 
     group by mile.product_id 
     ) b on b.product_id = last_milestone.product_id and b.maxDate = last_milestone.actual_date 
order by proj.id; 

내가 가진 문제는 모든 프로젝트는 최신 이정표가되며, 모든 프로젝트가 두 개 이상 것입니다 하나의 이정표. 왼쪽 조인을 시도했지만 프로젝트 당 여러 행을 반환합니다 (피할 필요가있는 항목 임).

필자는 Oracle 10을 사용하고 있으므로 PL/SQL에서 사용할 수있는 것이라면 그 점도 고려해 보겠습니다.

답변

4

사용 분석 :

SELECT v.* 
    FROM (SELECT proj.*, actual_date, 
       MAX(actual_date) over(PARTITION BY ms.product_id) last_milestone, 
       lag(actual_date) over(PARTITION BY ms.product_id 
            ORDER BY actual_date) previous_milestone 
      FROM zsof_projects proj 
      LEFT JOIN zsof_milestones ms ON ms.product_id = proj.id) v 
WHERE last_milestone = actual_date 
    OR (actual_date IS NULL AND last_milestone IS NULL) 

업데이트 :이 나는이 프로젝트는 이정표가없는 경우에 가입 왼쪽으로 가입 변형.

+0

고마워요! 그것은 잘 작동합니다. 나는 당신이 거기에서 사용했던 기능 중 일부를 보지 못했고 그래서 나는 약간의 독서가 있다고 본다. –

관련 문제