Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Help

Posted on 2006-11-14
4
2,618 Views
Last Modified: 2008-01-09
This is embarrasing but I'm having trouble with a SQL join I was hoping someone could assit with.

I'm writing a report (by item_id) that includes cost, po and invoice information. I'm fine with the item and cost data until I start joining to the po tables for the po data. Since I have to go to po_lines_all to complete the join from the item master to the po_header (for inventory_item_id), I'm now getting multiple records because of the individual po_lines. I don't see any way around this though. Can this not all be done through the same SQL statement?

Here is a small chunk of the code including the problematic join:

SELECT
msi.segment1                             item,
msi.organization_id                        org,
sum(qoh.transaction_quantity)                  qoh,            -- total quantity on hand
cic.item_cost                           item_cost,    -- frozen cost
max(poh.segment1)                        po_num        -- latest PO
FROM
mtl_system_items_b                         msi,
mtl_onhand_quantities                  qoh,
cst_item_costs                        cic,
po_headers_all                        poh,
po_lines_all                        pol
WHERE
msi.inventory_item_id          = qoh.inventory_item_id and
msi.inventory_item_id          = cic.inventory_item_id and
cic.cost_type_id                   = 1 and
cic.organization_id               = msi.organization_id and
pol.item_id                          = msi.inventory_item_id and
poh.po_header_id                = pol.po_header_id and
msi.organization_id              = 18 and
msi.inventory_item_id           = 2746
GROUP BY
msi.segment1,
msi.organization_id,
cic.item_cost
0
Comment
Question by:feign3
4 Comments
 
LVL 27

Expert Comment

by:sujith80
ID: 17938031
Does it help?

SELECT
msi.segment1                         item,
msi.organization_id                    org,
sum(qoh.transaction_quantity)               qoh,            -- total quantity on hand
cic.item_cost                       item_cost,    -- frozen cost
max(poh.segment1)                    po_num        -- latest PO
FROM
mtl_system_items_b                     msi,
mtl_onhand_quantities               qoh,
cst_item_costs                    cic,
po_headers_all                    poh,
(select distinct item_id, po_header_id  from po_lines_all  )                  pol
WHERE
msi.inventory_item_id          = qoh.inventory_item_id and
msi.inventory_item_id          = cic.inventory_item_id and
cic.cost_type_id                   = 1 and
cic.organization_id               = msi.organization_id and
pol.item_id                          = msi.inventory_item_id and
poh.po_header_id                = pol.po_header_id and
msi.organization_id              = 18 and
msi.inventory_item_id           = 2746
GROUP BY
msi.segment1,
msi.organization_id,
cic.item_cost
0
 
LVL 48

Expert Comment

by:schwertner
ID: 17938038
If I understand you correctly this join

poh.po_header_id                = pol.po_header_id

produces many lines.

You have to limit it somehow, e.g.

poh.po_header_id   IN (select  po_header_id from pol)
0
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 50 total points
ID: 17938221
Or, maybe you need a sub-query to get the max(po_header_id) from po_lines for that item, then remove the join to po_lines_all from the main query like this:

(Note that I also re-wrote the "where" clauses to put all of the known values on the right, and consistently use mtl_system_items as the "driving" table.)

SELECT
msi.segment1                         item,
msi.organization_id                    org,
sum(qoh.transaction_quantity)               qoh,            -- total quantity on hand
cic.item_cost                       item_cost,    -- frozen cost
max(poh.segment1)                    po_num        -- latest PO
FROM
mtl_system_items_b                     msi,
mtl_onhand_quantities               qoh,
cst_item_costs                    cic,
po_headers_all                    poh,
WHERE
qoh.inventory_item_id          = msi.inventory_item_id and
cic.inventory_item_id           =  msi.inventory_item_id and
cic.cost_type_id                   = 1 and
cic.organization_id               = msi.organization_id and
poh.po_header_id                = (select max pol.po_header_id from po_lines_all pol
                                              where pol.item_id = msi.inventory_item_id) and
msi.organization_id              = 18 and
msi.inventory_item_id           = 2746
GROUP BY
msi.segment1,
msi.organization_id,
cic.item_cost
0
 

Author Comment

by:feign3
ID: 17938458
Markgeer, yours was in the right ball park... It is the multiple po_line_id's that are producing the problem so I added:

pol.po_line_id = (select max(pol.po_line_id)
                         from po_lines_all pol
                         where pol.item_id = 2746)

This seems to have fixed things.

Thanks everyone!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question