如果有人可以提供帮助,那就太好了。我编写了以下查询,但除非我使用distinct,否则返回多行。使用distinct它可以按预期工作,但没有它非常慢并返回许多行。
SELECT distinct ('RCT' || rt.transaction_id) Unique_Receipt_Source_ID,
hla.location_code warehouse_code,
iwm.whse_name warehouse_name,
pvs.vendor_site_id vendor_site_id,
pvs.vendor_site_code vendor_site_name,
pha.vendor_id,
v.vendor_name,
pha.segment1 po_num,
pla.attribute4 container,
pla.attribute5 fill_level,
pha.fob_lookup_code inco_terms,
iwi.fixed_leadtime,
pla.line_num po_line_num,
iim.item_no item_no,
rt.transaction_date receipt_date,
rt.quantity Receipt_quantity,
rt.unit_of_measure Receipt_UOM,
rt.currency_code Receipt_Currency_Code,
(rt.quantity * pla.unit_price) receipt_amount,
pla.unit_price PO_Unit_price,
iim.item_desc1 item_description,
pla.item_description po_line_description,
pha.terms_id SYS_pay_terms_id,
ap.name pay_terms_name,
ap.description pay_terms_description,
papf.full_name BUYER,
plla.country_of_origin_code country_of_origin,
(pla.quantity * pla.unit_price) PO_LINE_AMOUNT,
pha.currency_code PO_currency_code,
pla.quantity PO_LINE_QTY,
pla.unit_meas_lookup_code PO_LINE_UOM
FROM po_headers_all pha,
po_line_locations_all plla,
po_lines_all pla,
apps.po_vendor_sites_all pvs,
apps.po_vendors v,
rcv_transactions rt,
apps.ic_whse_inv iwi,
apps.ic_whse_mst iwm,
apps.ic_item_mst iim,
apps.per_all_people_f papf,
apps.hr_locations_all hla,
ap_terms ap,
mtl_system_items_b msi
WHERE pha.po_header_id = pla.po_header_id
AND plla.po_header_id = pha.po_header_id
AND papf.person_id = pha.agent_id
AND pha.vendor_id = v.vendor_id
AND pha.vendor_site_id = pvs.vendor_site_id
AND v.vendor_id = pvs.vendor_id
AND pla.po_header_id = pha.po_header_id
AND iim.item_no = msi.segment1
and msi.inventory_item_id = pla.item_id
and iwi.item_id = iim.item_id
AND rt.po_header_id = pha.po_header_id
AND hla.location_id = pha.ship_to_location_id
AND rt.po_line_id = pla.po_line_id
AND iwm.whse_code = iwi.whse_code
AND hla.location_code = iwi.whse_code
AND pha.terms_id = ap.term_id
AND rt.transaction_type = 'RECEIVE'
order by pha.segment1, pla.line_num;
答案 0 :(得分:1)
此查询是ANSI连接语法优于隐式连接语法的一个很好的示例。 ANSI连接语法强制您一次构建一个结果集。保持表及其谓词靠近使得语句更容易 理解和调试。
原始查询需要记住数十个项目才能理解其中的任何一项。新语法需要了解集合,新表以及将其添加到集合的新谓词;重复这些步骤,直到查询完成。
下面的重写查询显示了ANSI连接语法的一些更具体的好处。当强制命令谓词时,很明显其中一个谓词被重复。该谓词是无意义的重复还是缺失条件的错字?将此语法与现代IDE相结合,可以快速找到错误;突出显示一个集合,运行它,检查结果,将下一个表格和谓词添加到突出显示,运行它,检查结果;重复这些步骤,直到找到错误。
SELECT *
FROM po_headers_all pha
JOIN po_line_locations_all plla
ON pha.po_header_id = plla.po_header_id
JOIN po_lines_all pla
ON pha.po_header_id = pla.po_header_id
AND pha.po_header_id = pla.po_header_id --Repeated predicate - typo?
JOIN apps.po_vendor_sites_all pvs
ON pha.vendor_site_id = pvs.vendor_site_id
JOIN apps.po_vendors v
ON pha.vendor_id = v.vendor_id
AND pvs.vendor_id = v.vendor_id
JOIN rcv_transactions rt
ON pla.po_line_id = rt.po_line_id
AND pha.po_header_id = rt.po_header_id
AND rt.transaction_type = 'RECEIVE' --Not a join, but may be helpful here.
JOIN apps.hr_locations_all hla
ON pha.ship_to_location_id = hla.location_id
JOIN apps.ic_whse_inv iwi
ON hla.location_code = iwi.whse_code
JOIN apps.ic_whse_mst iwm
ON iwi.whse_code = iwm.whse_code
JOIN mtl_system_items_b msi
ON pla.item_id = msi.inventory_item_id
JOIN apps.ic_item_mst iim
ON msi.segment1 = iim.item_no
AND iwi.item_id = iim.item_id
JOIN apps.per_all_people_f papf
ON pha.agent_id = papf.person_id
JOIN ap_terms ap
ON pha.terms_id = ap.term_id
ORDER BY pha.segment1, pla.line_num;