关于PO 和PR 的联系问题
以前一直想解决的就是输PO号,能购查询出来转化而来的PR 号,最开始的研究结果是:
采购单找到请购单
通过采购单找到请购单的过程非常麻烦:
/* Formatted on 2006/03/30 14:35 (Formatter Plus v4.8.6) */
SELECT pod.req_header_reference_num, prh.segment1
FROM
po_headers_all poh,
po_distributions_all pod,
po_req_distributions_all prod,
po_line_locations_all poll,
po_requisition_lines_all prl,
po_requisition_headers_all prh
WHERE prh.requisition_header_id = prl.requisition_header_id
AND prod.requisition_line_id = prl.requisition_line_id
AND prod.distribution_id = pod.req_distribution_id
AND poll.line_location_id = pod.line_location_id
AND poll.po_header_id = poh.po_header_id
AND poh.segment1 ='20500214'
涉及几个表相关联, 具体是:
po_headers_all poh, -- 采购单头
po_distributions_all pod, --采购单分部
po_req_distributions_all prod, --请购单分部
po_line_locations_all poll, --
--这个表做什么的,还不清楚, 但是如果连接采购单和请购单,必须通过此table
po_requisition_lines_all prl, --请购单行
po_requisition_headers_all prh --请购单行
再后来网友提示不需要采购单,请购单分布属性,那么就可以这样:
SELECT prh.segment1, pol.from_line_id, pol.*
FROM po_lines_all pol,
po_headers_all poh,
po_line_locations_all poll,
po_requisition_headers_all prh,
po_requisition_lines_all prl
WHERE pol.po_header_id = poh.po_header_id
AND poll.line_location_id(+)= prl.line_location_id
and prh.requisition_header_id=prl.requisition_header_id
AND poll.po_line_id = pol.po_line_id
and poll.po_header_id=poh.po_header_id
AND poh.segment1 ='20600018'
是可以满足要求了,那么,目前还有个问题就是, 如果PR转PO 后PO 又增加了行,那么如果连PR 查号码的时候就不能正确查出后增加的PO行,解决方法是外连接.上面的已经是外连接了,但是还是显示不出来,一定是还有外连接没有写.因为连接PO和PR 需要通过 po_line_locations_all 来连接, 那么就逐步测试一下:
Step 1:
SELECT pol.from_line_id, pol.* --prh.segment1,
FROM
po_headers_all poh,
po_line_locations_all poll,
-- po_requisition_headers_all prh,
-- po_requisition_lines_all prl,
/* full outer join */po_lines_all pol
WHERE pol.po_header_id = poh.po_header_id
--AND poll.line_location_id= prl.line_location_id
--and prh.requisition_header_id=prl.requisition_header_id
AND poll.po_line_id = pol.po_line_id
and poll.po_header_id=poh.po_header_id
AND poh.segment1 ='20600018'
可以正确查出po 的行.
还有一种简单简单的办法,就是把pr_line和pr_header表连在一起,用括号括起来,作为一个子查询,去和po_line_locations_all做外关联
--以上是网友ideal 的提议.
Ideal 说:
你连接得不对。不知要连pr_line表,其他的地方也要加上外联结
--他说的原因.我想也是这样,一定是哪里有外连接,而我并不清楚.
所以还是采用他的建议;就是把pr_line和pr_header表连在一起,用括号括起来,作为一个子查询,去和po_line_locations_all做外关联
关于外连接以前只是字面上的理解,现在遇到了,才知道数据库的基础知识重要.再次感慨一下.
顺遍说一下, 在9i 之前,oracle 不支持用单词写的外连接.如,
SELECT pol.from_line_id, pol.* --prh.segment1,
FROM
po_headers_all poh,
po_line_locations_all poll,
po_requisition_headers_all prh,
po_requisition_lines_all prl,
/* full outer join */po_lines_all pol
WHERE pol.po_header_id = poh.po_header_id
AND poll.line_location_id(+)= prl.line_location_id
and prh.requisition_header_id=prl.requisition_header_id
AND poll.po_line_id = pol.po_line_id
and poll.po_header_id=poh.po_header_id
AND poh.segment1 ='20600018'
其中 full outer join 在ERP应用的8.147 DB里并不被支持,所以还要写+那种origin 方法.
Step 2:
--step 2
SELECT prh.segment1,pol.from_line_id, pol.*
FROM
po_headers_all poh,
po_line_locations_all poll,
po_requisition_headers_all prh,
po_requisition_lines_all prl,
po_lines_all pol
WHERE pol.po_header_id = poh.po_header_id
AND poll.line_location_id(+)=(select prl.line_location_id
from prh, prl
where prh.requisition_header_id=prl.requisition_header_id)
AND poll.po_line_id = pol.po_line_id
and poll.po_header_id=poh.po_header_id
AND poh.segment1 ='20600018'
出现错误:
ORA-01799 a column may not be outer-joined to a subquery
Cause: expression(+) relop (subquery) is not allowed.
Action: Either remove the (+) or make a view out of the subquery. In V6 and before, the (+) was just ignored in this case.
Step 3:
--step 3
/* Formatted on 2006/04/07 14:01 (Formatter Plus v4.8.6) */
SELECT prl_line.segment1,pol.from_line_id, pol.* --prh.segment1,
FROM po_headers_all poh,
po_line_locations_all poll,
/*po_requisition_headers_all prh,
po_requisition_lines_all prl,*/
(SELECT prl.*, prh.segment1
FROM po_requisition_headers_all prh, po_requisition_lines_all prl
WHERE prh.requisition_header_id = prl.requisition_header_id) prl_line,
po_lines_all pol
WHERE pol.po_header_id = poh.po_header_id
AND poll.line_location_id= prl_line.line_location_id(+)
AND poll.po_line_id = pol.po_line_id
AND poll.po_header_id = poh.po_header_id
AND poh.segment1 ='20600018'
这样写是可以了
这一步是第二步的改善.关于外连接,如果那边的栏位缺少就在哪边写+号.
这个采购单的报表终于开发完了,学到了很多东西.
还没有解决的问题,如果使用动态参数输入工具列里的pl/sql 代码.因为主要攻外连接实现了请购单号的问题.但还存在不足,就是data block的分组问题,如果后加的采购单行要分别求和.
还要感慨一下,不是写报表时调field 和text(我以前粗略地称items) 的繁琐, 而数据库基础的重要性.pl/sql 的编程能力, 这些都必须提高!
以下是这个报表的sql 语句,供借鉴:
SELECTDISTINCT(ph.segment1) pono, pv.vendor_name vendor,
ph.fob_lookup_code fob, ph.ship_via_lookup_code via,
ap.NAME term_description,
NVL (pll.promised_date, pll.need_by_date) pr_date,
pl.line_num po_lineno, pll.shipment_num po_shipment,
-- msi.segment1 item_no,
NVL (msi.must_use_approved_vendor_flag,'N') avl_flag,
msi.segment1 || ' ' || pl.item_description descrip,
pll.quantity - NVL (pll.quantity_cancelled,0) qty,
NVL (pl.attribute1, pl.unit_meas_lookup_code) i_uom,
ph.currency_code currency, pl.unit_price u_price,
DECODE (ph.vendor_contact_id,
NULL,'',
pvc.last_name || pvc.first_name
) contact_name,
ROUND (( (pll.quantity - NVL (pll.quantity_cancelled,0))
* pl.unit_price
),
4
) line_total,
ph.comments v_desc, pll.line_location_id location_id,
pv.vendor_id v_vendor_id, ph.vendor_site_id v_site_id,
pl.item_id item_id, pl.line_type_id line_type,
prl_line.segment1 req_num
FROM po_vendors pv,
po_vendor_sites_all pvs,
po_vendor_contacts pvc,
po_headers_all ph,
po_lines_all pl,
ap_terms ap,
po_line_locations_all pll,
mtl_system_items msi,
(SELECT prl.*, prh.segment1
FROM po_requisition_headers_all prh,
po_requisition_lines_all prl
WHERE prh.requisition_header_id = prl.requisition_header_id) prl_line
WHERE pv.vendor_id = ph.vendor_id
AND pvs.vendor_site_id = ph.vendor_site_id
AND( ph.vendor_contact_id = pvc.vendor_contact_id
OR ph.vendor_contact_id ISNULL
) --best
AND ph.terms_id = ap.term_id
AND ph.org_id = NVL (:p_org_id,141)
AND ph.po_header_id = pl.po_header_id
AND pl.org_id = ph.org_id
AND pl.po_line_id = pll.po_line_id
AND pl.po_header_id = pll.po_header_id
AND pll.org_id = pl.org_id
AND pl.item_id = msi.inventory_item_id
AND pl.org_id = msi.organization_id
AND pll.quantity - NVL (pll.quantity_cancelled,0)>0
AND ph.segment1 IN
(NVL (:p_segment1, ph.segment1),
NVL (:p_segment2,''),
NVL (:p_segment3,''),
NVL (:p_segment4,''),
NVL (:p_segment5,''),
NVL (:p_segment6,''),
NVL (:p_segment7,''),
NVL (:p_segment8,''),
NVL (:p_segment9,''),
NVL (:p_segment10,''))
AND pll.line_location_id = prl_line.line_location_id(+)--notice here :outer join
AND pll.po_header_id = ph.po_header_id
ORDERBY pl.line_num , pll.shipment_num, ph.segment1;