View: po_item_consol_view

Columns
  Column Name Data Type
1.   current_po_key decimal(15)
2.   line_id tinyint
3.   po_item_line_descriptor_key decimal(15)
4.   orig_po_key decimal(15)
5.   owning_po_key decimal(15)
6.   pr_item_line_descriptor_key decimal(15)
7.   account_key decimal(15)
8.   organization_key decimal(15)
9.   reference varchar(25)
10.   description varchar(128)
11.   project_key decimal(15)
12.   task_key decimal(15)
13.   item_key decimal(15)
14.   uom_key decimal(15)
15.   person_key decimal(15)
16.   rate decimal(15,5)
17.   control_quantity char(1)
18.   closed_date timestamp
19.   po_item_line_key decimal(15)
20.   line_po_key decimal(15)
21.   begin_date timestamp
22.   end_date timestamp
23.   required_by_date timestamp
24.   internal_comments varchar(2000)
25.   external_comments varchar(2000)
26.   vi_overage char(1)
27.   quantity decimal(38,6)
28.   amount decimal(38,2)
Table/Column Dependencies
Table Column
po_item_line amount
  begin_date
  end_date
  external_comments
  internal_comments
  po_item_line_descriptor_key
  po_item_line_key
  po_key
  quantity
  required_by_date
  vi_overage
po_item_line_descriptor account_key
  closed_date
  control_quantity
  description
  item_key
  line_id
  organization_key
  orig_po_key
  person_key
  po_item_line_descriptor_key
  po_key
  pr_item_line_descriptor_key
  project_key
  rate
  reference
  task_key
  uom_key
purchase_order document_number
  mod_number
  orig_po_key
  po_key
  pr_key
  status
SQL Server Create Statement
create view po_item_consol_view
as
select
    po.po_key as current_po_key,
    descr.line_id,
    descr.po_item_line_descriptor_key,
    descr.orig_po_key,
    descr.po_key owning_po_key,
    descr.pr_item_line_descriptor_key,
    descr.account_key,
    descr.organization_key,
    descr.reference,
    descr.description,
    descr.project_key,
    descr.task_key,
    descr.item_key,
    descr.uom_key,
    descr.person_key,
    descr.rate,
    descr.control_quantity,
    descr.closed_date,
    line.po_item_line_key,
    line.po_key line_po_key,
    line.begin_date,
    line.end_date,
    line.required_by_date,
    line.internal_comments,
    line.external_comments,
    line.vi_overage,
    summary.quantity,
    summary.amount
from purchase_order po
join (
     select history.po_key,
        line.po_item_line_descriptor_key,
        max(history.mod_number) mod_number,
        max(case when history.mod_number is null then history.document_number else null end) document_number,
        sum(line.quantity) quantity,
        sum(line.amount) amount
    from po_item_line line
    join (
          select po.po_key po_key,
               po.pr_key,
               history.po_key predecessor_key,
               history.mod_number,
               history.document_number
        from purchase_order po
    join purchase_order history on history.orig_po_key = po.orig_po_key
          where (
                    po.po_key = history.po_key
        or (
            po.status in ('APPROVED', 'OPEN', 'CLOSED')
            and history.status in ('APPROVED', 'OPEN', 'CLOSED')
            and history.mod_number < po.mod_number
            )
        or (
            po.status not in ('APPROVED', 'OPEN', 'CLOSED')
            and history.status in ('APPROVED', 'OPEN', 'CLOSED')
                         )
                    )
    ) history on history.predecessor_key = line.po_key
     group by history.po_key,
          line.po_item_line_descriptor_key
     ) summary on summary.po_key = po.po_key
join purchase_order po_family on po_family.orig_po_key = po.orig_po_key
     and (
          po_family.document_number = summary.document_number
          or (
               po_family.mod_number = summary.mod_number
               and summary.document_number is null
               )
          )
join po_item_line line on line.po_item_line_descriptor_key = summary.po_item_line_descriptor_key
     and line.po_key = po_family.po_key
join po_item_line_descriptor descr on descr.po_item_line_descriptor_key = summary.po_item_line_descriptor_key