查询项目下的BU预算和实际PO金额及TBD

WHILE monthly_value <= 4000
LOOP
     monthly_value := daily_value * 31;
END LOOP;

select t.project_id,
       t.category1,
       t.amount,
       case
         when t.type = 1 then
          (select sum(p.po_amount)
             from PO_LINE_ALL p, budget b
            where p.project_id = b.project_id(+)
              and p.category1 = b.category1(+)
              and b.category1 is null
              and p.project_id = t.project_id)
         else
          t.po_amount
       end as po_amount
  from (select nvl(b.project_id, p.project_id) project_id,
               nvl(b.category1, p.category1) category1,
               b.amount,
               p.po_amount,
               case
                 when b.project_id is not null then
                  decode(b.category1, 'TBD', 1, 0)
                 else
                  2
               end as type
          from budget b
          FULL OUTER JOIN PO_LINE_ALL p
            ON (b.project_id = p.project_id and b.category1 = p.category1)) t
 order by t.project_id, t.type, t.category1;

你可能感兴趣的:(查询)