create view pr_labor_consol_all_view
as
select
descr.line_id,
descr.pr_labor_line_descriptor_key,
line.pr_labor_line_key,
descr.orig_pr_key,
line.pr_key as line_pr_key,
descr.pr_key as owning_pr_key,
descr.account_key,
descr.organization_key,
descr.reference,
descr.description,
descr.project_key,
descr.task_key,
descr.labor_category_key,
descr.person_key,
descr.rate,
descr.control_hours,
line.begin_date,
line.end_date,
line.internal_comments,
line.external_comments,
summary.hours,
summary.amount
from pr_labor_line_descriptor descr
join pr_labor_line line on line.pr_labor_line_descriptor_key = descr.pr_labor_line_descriptor_key
join purchase_requisition pr on pr.pr_key = line.pr_key
join (
select line.pr_labor_line_descriptor_key,
max(pr.mod_number) mod_number,
max(case when pr.mod_number is null then document_number else null end) document_number,
sum(line.hours) hours,
sum(line.amount) amount
from pr_labor_line line
join purchase_requisition pr on pr.pr_key = line.pr_key
and pr.status != 'CANCELED'
group by line.pr_labor_line_descriptor_key
) summary on summary.pr_labor_line_descriptor_key = line.pr_labor_line_descriptor_key
where summary.mod_number = pr.mod_number
and (
(
summary.document_number is null
and summary.mod_number = pr.mod_number
)
or summary.document_number = pr.document_number
)
|